Switching between unnamed arguments and standard input: thoughts?

  • Thread starter Thread starter Alex K. Angelopoulos [Server MVP]
  • Start date Start date
A

Alex K. Angelopoulos [Server MVP]

I'm working on a standard way to allow argument-vs-input stream selection
for console-mode WSH scripts, and would like some opinions about the best
way to proceed with them.

I've finally rejected the idea of automatically redirecting to stdin in the
absence of unnamed arguments due to how often it makes sense to use a
default value of some kind (for example, getting uptime - no args or input
would mean get it from the local system usually. On the other hand, you may
want to check uptime data for an entire list of remote systems, in which
case you want to read them from stdin).

I'm now using a strategy of checking for a null named argument, which would
be submitted on the commandline as just a solidus (/) with nothing following
it. This allows making it a "named argument" analogous to the Unix "-"
option name used for redirection of input.

I also drop all unnamed arguments which could be considered input data if
stdin is being read. This seems to me to be the most logical way to handle
things, but I'd like to get some feedback from other people using
commandline tools and stdin streams about whether it makes sense to do it
this way.

Any opinions? WSF content I use for this follows...

====================
Implementation Info

For example, here's how the option would be specified in the <runtime>
element - note the embedded backspace character (0x08) which forces display
of a single "/" in the usage help:

<named name="/" helpstring="causes data to be read from stdin; each line is
interpreted as a separate script name." required="false" type="simple"/>

Within VBScript code, the actual test would look like this:

If WScript.Arguments.Named.Exists(vbNullString) Then
Do While Not WScript.StdIn.AtEndofStream
Main WScript.StdIn.ReadLine
Loop
Else
For Each arg in WScript.Arguments.UnNamed
Main arg
Next
End If
 
IMHO all input comes from stdin except for the possible command line
argument of /? to be consistent with the MS help syntax. Passing
parameters from the command line is a pita because of the DOS parsing
of special characters. Readline simply passes you the intact sysin
string minus crlf. Unfortunately the gotcha with this approach
AtEndofStream requires something even if nothing more than a Ctrl-Z
from the keyboard if no piped input. I have a .js script similar to
Unix Xargs where null command line arguments simply echoes stdin else
stdin is appended to command line arguments which is normally a cmd /c
to remind me of what I'm doing and required for the .run method to
create a separate Dos environment. In this case the one shoe fits all
approach is ultimately determined by the intended use and audience.

BitsNotBytes
 
BitsNotBytes said:
IMHO all input comes from stdin except for the possible command line
argument of /? to be consistent with the MS help syntax. Passing
parameters from the command line is a pita because of the DOS parsing
of special characters.

Actually, the syntactic issues can be worked around - named arguments are
way too convenient to abandon just to make that easier!

All you need to do is either escape special characters with a caret (^) or
quot them. For example, in the following examples, the pipe character is
correctly interpreted as an argument:

cscript args.vbs "|" a b c
cscript args.vbs ^| a b c

The one residual issue with WSH scripts is the fact that ALL of the
double-quotes are stripped from arguments as they are put into the arguments
collections. To handle that, if it's possibly going to be a problem I allow
one of two alternate syntaxes for this. One is to allow use of backticks in
place of literal quotes and replace them; the other is to run "unescape"
over the arguments, so literal doublequotes can be entered as %22. JScript
has a similar function you can use for that, of course.
Readline simply passes you the intact sysin
string minus crlf. Unfortunately the gotcha with this approach
AtEndofStream requires something even if nothing more than a Ctrl-Z
from the keyboard if no piped input.

Yeah. That's the nasty I'm trying to get around, particularly since I do a
lot of batch testing in an IDE that chokes on stdin redirects during
captures.
.. I have a .js script similar to
Unix Xargs where null command line arguments simply echoes stdin else

Now an xargs clone is an interesting idea!
 
Beware the pipe.

Have all 'managed'.
dblquoted.

"a" "b" "c"

will e.g.

var cmdL = top.oHta.commandLine
var arr = cmdL.replace(/" "/igm,'"\r\n"').replace(/"/g,"").split("\r\n")
-------

when you not send a dblQuote in an argument,
which alternatively should be sent via saved file.
-------
-Same as certain characters will not xecute in xml.
=========
"Switching between unnamed arguments and standard input: thoughts?"

There is no diff on win level. Named && Unnamed are collections.

"standard input"

No S&P or Standards500 here, find the mass solution here.
 
Back
Top