Command line parameters, standard aproach to parse

2019-06-24 12:32发布

问题:

I've been reading a bit about how programs handle command line parameters. But the information seems to be "incomplete", things I've read:

  • Options may have a preceding '-' or '/' sign if front of them.
  • Options can have additional arguments (which go without a - sign)
  • the option arguments follow the option directly, with or without a space.
  • Options can be a single letter or a full word.
  • optionscan be merged inside a single "option": -abc equals -a -b -c

(Source)

Now I'm really wondering: What kind of options do you give a "-" sign and which not. Also the merging of options into 1 seems to be incompatible with full-word options? "-file" can be a full word, but it might also mean "-f", "-i", "-l", "-e", 4 different switches. Or even: "-f" with "ile" as option_argument.

Am I understanding something wrong?

回答1:

On systems like Linux, there is the convention that options that are full words use two dashes (e.g. --file), while single-letter options use a single dash (e.g. -f.)

Using slash to introduce options is from old DOS, and is being kept in Windows.

Also, if an option is using a whole word, it can not be split into several options. This is in regards to your example with -file: -file can either be one option, or four different options (-f, -i, -l and -e).

All in all, how options looks, or is handled, differ very much between programs, and there really isn't any specific standard.

I would suggest you find some way you like, and then use that.



回答2:

There are no Windows standards for options/arguments/parameters etc.

Options are developed as per the developers understanding to make it easy for them to interpret the passed in information.

The real reason for / or - prefixed options is generally for parsing rules since it's much easier to parse unique option prefixes with less code rather than complex options with large code. Common development standards (such as standard use options) minimises confusion for users ;)