How to format command line argument arguments

A typical format for command line arguments is:

myApp --myArg=myValue 

What if I want to pass a set of key value pairs through the command line? Something like:

 myApp --myList={arg1=val1;arg2=val2;arg3=val3...} 

Since there seems to be no such standard for this type , can anyone provide examples of well-used utilities that have this input of command line arguments? I poked a few pages, but could not find.

Edit: I wonder how the input should be formatted and what the help text looks like.

+7
command-line standards format command-line-arguments
source share
2 answers

I think it depends a lot on how you parse the arguments in your program.

Here are some examples of how programs accept multiple key-value pairs.

man php:

  --define foo[=bar] -d foo[=bar] Define INI entry foo with value bar 

man git:

  -c <name>=<value> Pass a configuration parameter to the command. The value given will override values from configuration files. The <name> is expected in the same format as listed by git config (subkeys separated by dots). 

For both, you can pass multiple -d or -c arguments to programs that give you the opportunity to provide a list of key-value pairs for programs.

IMO, this is not a big problem when you have your own style of receiving lists of key-value pairs for your program while it works and is well documented. :)

PS: I think this question would be more appropriate to be posted on the Stack Exchange program , and not on SO. See here and here .

+5
source share

If an application requires so many arguments, I would use a configuration file instead of passing them on the command line:

myApp --config=file.cnf

This approach has the following advantages:

  • flexibility - you can have a bunch of configurations prepared for different calls, and just use them,
  • no citation issues - it always hurts if the command line arguments have spaces, double quotes, <,> or other special characters,
  • simplicity - you control the format of the configuration file, it can be, for example, INI, JSON, XML, etc. It’s easy to create and easy to parse using command line parsing - if not simpler
  • security - if any argument can be sensitive, it is not displayed from tools that display command line arguments.
+2
source share

All Articles