34

Just using kubectl as an example, I note that

kubectl run --image nginx ...

and

kubectl run --image=nginx ...

both work.

For command-line programs in general, is there a rule about whether an equals sign is allowed/required between the option name and the value?

AdminBee
  • 22,803

2 Answers2

30

In general, the implementation of how command-line arguments are interpreted is left completely at the discretion of the programmer.

That said, in many cases, the value of a "long" option (such as is introduced with --option_name) is specified with an = between the option name and the value (i.e. --option_name=value), whereas for single-letter options it is more customary to separate the flag and value with a space, such as -o value, or use no separation at all (as in -oValue).

An example from the man-page of the GNU date utility:

  -d, --date=STRING
        display time described by STRING, not 'now'
  -f, --file=DATEFILE
        like --date; once for each line of DATEFILE

As you can see, the value would be separated by a space from the option switch when using the "short" form (i.e. -d), but by an = when using the "long" form (i.e. --date).

Edit

As pointed out by Stephen Kitt, the GNU coding standard recommends the use of getopt and getopt_long to parse command-line options. The man-page of getopt_long states:

A long option may take a parameter, of the form --arg=param or --arg param.

So, a program using that function will accept both forms.

AdminBee
  • 22,803
  • libpopt erroneously printed the equals sign against short options, resulting in usage messages that were not actually the command-line syntax accepted by the program. http://jdebp.uk./Softwares/libpopt/ – JdeBP Mar 17 '20 at 13:51
  • 1
    Maybe getopt can be treated as a reference point, if not a standard so that allowing either with or without the equals sign is normal with long options; with short options, no equals sign is used. – Joshua Fox Mar 17 '20 at 14:27
  • 1
    Yes, I think it is reasonably safe to say so. – AdminBee Mar 17 '20 at 14:28
  • @JdeBP fyi, the 1st link on your libpopt page is broken –  Mar 17 '20 at 15:01
10

For command-line programs in general, is there a rule about whether an equals sign is allowed/required between the switch and the value?

No, there isn't. There are many competing standards in the open source world and computing in general (obligatory xkcd) and everyone can come up with new rules and standards any time they want. POSIX Utility Argument Syntax for example doesn't mention = at all for example while man getopt mentions it. In practice you can come across all kinds of command line programs:

Those that take long option value after = or after a whitespace:

$ touch a b c d
$ ls --format=verbose
total 0
-rw-r--r-- 1 ja users 0 Mar 17 14:39 a
-rw-r--r-- 1 ja users 0 Mar 17 14:39 b
-rw-r--r-- 1 ja users 0 Mar 17 14:39 c
-rw-r--r-- 1 ja users 0 Mar 17 14:39 d
$ ls --format verbose
total 0
-rw-r--r-- 1 ja users 0 Mar 17 14:39 a
-rw-r--r-- 1 ja users 0 Mar 17 14:39 b
-rw-r--r-- 1 ja users 0 Mar 17 14:39 c
-rw-r--r-- 1 ja users 0 Mar 17 14:39 d

Those that do not take long option value after = but require a whitespace:

$ readelf  -a main | grep  'program interpreter'
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
$ patchelf --set-interpreter=fake main
patchelf: getting info about '--set-interpreter=fake': No such file or directory
$ patchelf --set-interpreter fake main
$ readelf  -a main | grep  'program interpreter'
      [Requesting program interpreter: fake]

Those that take value after = but do not take options with - or --:

dd if=/dev/urandom of=~/Desktop/test.txt bs=1M count=3

There can be many reasons for a which a given command line program accepts input in a given way: author's vision, because nobody cares, because author didn't know that someone else has already come up with a standard, because program has been ported onto Unix from different operating system with completely different conventions or has been made to look like it has been.

  • 1
    Wow, allowing options without the dash/double-dash is a mess. – Joshua Fox Mar 17 '20 at 14:29
  • @JoshuaFox: yep, it is. Ideally, you should always read manual before using a new command to know how it behaves. – Arkadiusz Drabczyk Mar 17 '20 at 14:30
  • 3
    I think that dd is a redherring and bringing it up is only muddling the point, as those foo=bar of dd are operands not options. There are other programs which use arguments of the key=value form (awk, make, env, etc), and they may also accept options/switches (= command line arguments starting with a dash). Standard dd does not accept any command line options, though GNU dd does (2 options: --help and --version). –  Mar 17 '20 at 15:07
  • 1
    @mosvy: man dd from coreutils mentions both operands and options, man dd on FreeBSD uses "operand" and "option" interchangeably and I really don't understand what's the difference between operands and options - I guess that maybe GNU people used the term operand for historical compatibility as they don't start with a minus. – Arkadiusz Drabczyk Mar 17 '20 at 15:13
  • And patchelf does not accept --set-interpreter=bar because it's hand-parsing the options, instead of using getopt_long. Other examples which use long options and do NOT follow the GNU convention are classical Xtoolkit & other X11 apps –  Mar 17 '20 at 15:14
  • @mosvy: yes, I know, parsing in patchelf is done in src/patchelf.cc. You mention it because you think this information should be added to my answer? – Arkadiusz Drabczyk Mar 17 '20 at 15:15
  • 2
    @ArkadiuszDrabczyk the options accepted by GNU dd are --help and --version, as already mentioned. The difference between options and other arguments/operands/etc is made by the standard, I didn't invented it ;-) –  Mar 17 '20 at 15:15
  • I mention all that because I consider it relevant and interesting. –  Mar 17 '20 at 15:16
  • @mosvy: what standard? If it's GNU, ok, they may have a standard. I tried to be clear in my answer and let user know that they may be multiple standards for the same thing. See dd manpage from FreeBSD: https://www.freebsd.org/cgi/man.cgi?query=dd(1). They use operand and option interchangeably. It doesn't matter in the long run but only because GNU people have come up with some standard does not mean that the world will follow. – Arkadiusz Drabczyk Mar 17 '20 at 15:18
  • "what standard?" POSIX, see my 1st options link above. AFAIK GNU dd conforms to it, though it implements its own extensions. –  Mar 17 '20 at 15:19
  • @mosvy: the page you linked to clearly says that dd doesn't understand options as understood by POSIX. Other standards/people may have a different understanding of what option means. FWIW, in my answer I've also linked to POSIX standard that doesn't even mention option=val even though many programs accept it, not only GNU ones. That's the thing with the standards - there are many of them and they commonly contradict each other. – Arkadiusz Drabczyk Mar 17 '20 at 15:23
  • The GNU standard and other conventions followed by BSDs or other Unix systems in this matter do not contradict the POSIX standard AFAIK. Please do not equivocate just to "win" what you think is an "argument": of course there are a lot of other meanings of the word "options" (eg. "stock options"), but here "command line options/flags/switches" = "command line arguments starting with a dash". Reference. –  Mar 17 '20 at 15:31
  • @mosvy: I'm not trying to win a discussion that cannot be won because we're not discussing something precise enough to define it clearly. FreeBSD dd manpage and Wikipedia article I linked to in my answer say option sometimes when referring to dd operands and it's good as long as long user understands what it means in the given case. – Arkadiusz Drabczyk Mar 17 '20 at 15:35
  • You should submit a documentation bug report to FreeBSD, and edit the Wikipedia, and make them use a consistent terminology. –  Mar 17 '20 at 15:37