I write lots of command line tools, mainly in perl and C#, and am pretty rigorous about providing good command-line support to allow setting of tunable parameters and the like. My command-line processing (in perl) typically looks like:
...
foreach ( @ARGV )
{
if ( m/^--help/i )
{
print STDERR <<EOF;
Usage: $0 [options]
--help Show this brief help listing
--verbose Show a bit more runtime debugging
--blah=ABC Set the blah option to ABC
EOF
exit 0;
}
elsif ( m/^--verbose/ )
{
$verbose++;
}
.... more options here
}
...
Sometimes I'll have more than a dozen options for setting this or that, and there's typically a shell script that calls it with the options that apply to the task at hand:
exec my-program \
--verbose \
--blah=FOO \
--port=22 \
--no-delete \
...
Using a wrapper script means that my testing will be a bit more consistent, using the same options from run to run. But what happens if I want to run this script without with the --blah=FOO parameter?
Well I can't just put a shellscript # sign at the start of the option because it's actually part of the continuation from the prior lines. In practice I normally move the line elsewhere, commented out:
# --blah=FOO «-- commented-out option
exec my-program \
--verbose \
--port=22 \
--no-delete \
...
But this gets tedious if one has to shuffle options around a lot for testing.
The practice I've developed is to process the command line and look for three dashes to start an option, which effectively comments it out:
foreach ( @ARGV )
{
if ( m/^---/ )
{
# ignored option
next;
}
elsif ( ... )
{
...
}
...
}
Which makes the script now look like:
exec my-program \
--verbose \
---blah=FOO \ «-- commented out option
--port=22 \
--no-delete \
...
and to un-comment it, I just delete one of the dashes.
And for Windows-style slash parameters, the token /-... does the same thing (noting that the ^ is a line-continuation character in a MS-DOS batch file:XTSend.exe ^ /-simulated-send ^ «-- commented out option /verbose ^ /count:402 ^ /xtsdb:"database-connection-string-here"
This has been far more useful than I expected, so I've adopted it for essentially any program that has command-line processing.





Comments