Re: Command-Line Arguments

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ralph (nt_consulting32_at_hotmail.com)
Date: 02/24/04


Date: Tue, 24 Feb 2004 11:44:08 -0600


"John" <anonymous@discussions.microsoft.com> wrote in message
news:BAC6D939-812C-489E-B487-CCC29CE4ED18@microsoft.com...
> I use a very simple method of parsing the command-line to get the
arguments, which are currently assumed to be space delimited. However, I
need to make a change to one of my utilities to accept a fully qualified
filename, including the path. This requirements means that I can no longer
use space delimited arguments because paths and filenames can have spaces.
>
> What is the best method of passing arguments when spaces are integral to
one or more of the arguments (i.e., filenames, paths, etc.)?
>
> Thanks,
> John

John,

You will save yourself (and your clients) some time and trouble if you adopt
from the beginning a "schema" or standard convention for handling command
line arguments.
There are 3 basic forms:
1) A traditional 'C' form where you have the executable and each of the
arguments.
            example: 'exe file1 file2 ...'
           With this form "spaces" were considered 'native' delimiters and
anything with
            spaces had to be quoted.
2) The Dos/VMS form where you can present Options with values.
           Options are delimited with a '/' and the value assigned with a
'='
           Single options are supported without the '=' sign.
           "exe /USER=value /NAME="Conan the barbarian" /I"
           Strings were handled in two ways - force using quotes or
           capturing everything until you came to the next flag '/'.
           (In this schema a '-' was often used to negate a particular
option.)
3) The UNIX or AT&T getopts form with options and values.
           Options are delimited with the '-' flag
            and if any values they are assigned directly behind the key.
           Options could be combined as in "-arw -d"
           Only single characters were supported as options
           colons were then used to indicate string values.
           Example: 'exe -U23 -N:Conan the barbarian -i

IHMO It is important to accept one of these scheme and stick to it.

You should also always provide a "/?" option which when given will output a
Help string. You can easily use this as the correct response for any
parameters errors.

Also there is nothing wrong with requiring your user to use quotes to
correctly enter whole values with spaces.

HTH
-ralph



Relevant Pages

  • Re: Command-Line Arguments
    ... > I use a very simple method of parsing the command-line to get the ... use space delimited arguments because paths and filenames can have spaces. ...
    (microsoft.public.vb.general.discussion)
  • Re: Command-Line Arguments
    ... > I use a very simple method of parsing the command-line to get the arguments, ... arguments because paths and filenames can have spaces. ...
    (microsoft.public.vb.general.discussion)
  • Re: Compare 2 PAS files
    ... Gerrit Beuze wrote: ... Not in any of your documentation that I can see. ... states that the command-line can take one or two filenames as ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Any Chinese Lispers here?
    ... Well, I'm on Windows, and accentuated letters (e.g. e acute) between ... the command-line and GUI applications, as well as in filenames when ...
    (comp.lang.lisp)
  • Re: Command-Line Arguments
    ... double quotes around strings that contain spaces. ... > I use a very simple method of parsing the command-line to get the ... use space delimited arguments because paths and filenames can have spaces. ...
    (microsoft.public.vb.general.discussion)