12

A typical format for command line arguments is:

myApp --myArg=myValue

What if I want to pass in 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 standard for this sort of thing, can anyone provide examples from well-used utilities that have this sort of command line argument input? I poked around some man pages but didn't find any.

Edit: I'm wondering both how the input should be formatted and what the help text might look like.

Community
  • 1
  • 1
jtpereyda
  • 6,987
  • 10
  • 51
  • 80

2 Answers2

8

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

Here are some examples that the programs accept multiple key-value pair values.

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, one can pass multiple -d or -c arguments to the programs which gives you the ability to supply a list of key-value pairs to the programs.

IMO, it's not a big problem having your own style of accepting lists of key-value pairs for your program as long as it works and is well-documented. :)

P.S.: I think this question would be more appropriate be placed on Programmers Stack Exchange rather than on SO. See here and here.

Community
  • 1
  • 1
cychoi
  • 2,890
  • 3
  • 22
  • 31
5

If the app needs so many arguments, I would use a config file instead of passing them in command line:

myApp --config=file.cnf

This approach has the following advantages:

  • flexibility - you can have a bunch of configs prepared for different invocations, and just use them,
  • no problems with quoting - it's always painful if command line arguments have spaces, double quotes, <, >, or other special characters,
  • simplicity - you control the config file format, it can be for example INI, JSON, XML etc. It's easy to create it and as easy to parse as parsing command line -- if not easier,
  • security - if any argument may be sensitive, it's not visible from tools displaying command line arguments.
Endrju
  • 2,354
  • 16
  • 23