Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Format Command Line Argument Key Value Pairs

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.

like image 611
jtpereyda Avatar asked Aug 07 '13 23:08

jtpereyda


People also ask

What is key value pair format?

A key-value pair consists of two related data elements: A key, which is a constant that defines the data set (e.g., gender, color, price), and a value, which is a variable that belongs to the set (e.g., male/female, green, 100). Fully formed, a key-value pair could look like these: gender = male. color = green.

What is command line arguments give an example of it?

By show, argv[0] is the order with which the program is conjured. argv[1] is the principal command-line argument. The last argument from the order line is argv[argc – 1], and argv[argc] is consistently NULL.


1 Answers

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.

like image 181
cychoi Avatar answered Oct 18 '22 10:10

cychoi