Command parameters & arguments - Correct way of typing?
Some programs do accept only one style. One or the other.
Anything that uses getopt()
should accept both -xfoo
and -x foo
for option -x
that takes an argument. However, not all programs use getopt()
. Optional arguments are another thing yet, they might not work with -x foo
, but may require -xfoo
, because otherwise it would be hard to give options without the argument.
As for which one you "should" use, that's a question of preference where everyone is likely to have their own opinion.
Some programs require one style, others don't. For example:
mysql -ph 10.1.1.1
will make mysql
try to connect to localhost
with password h
.
mysql -p -h 10.1.1.1
on the other hand will connect to 10.1.1.1
and prompt for password.
That is up to the utility to define. Some accept one or both of the forms.
In POSIX utility conventions, the argument syntax section gives this template
utility_name[-a][-b][-c option_argument]
[-d|-e][-f[option_argument]][operand...]
and requires that option-arguments be separated from their options by blank characters, except when the option-argument is optional, in which case no separation should be used. But then...
there are some exceptions in POSIX.1-2017 to ensure continued operation of historical applications.
and in the next section, utility syntax guidelines, guideline 7 says that option-arguments should not be optional.
If one thing is clear from all that confusion is that there is not anything near a written in stone rule about the blanks between flags and arguments.
Concrete examples to get the message accross:
Syntax error Syntactically correct
xclip -ir <<< ABC xclip -i -r <<< ABC
xkbcomp -I /home/user "main" :0 xkbcomp -I/home/user "main" :0
rename -e's/f/x/' f rename -e 's/f/x/' f
As a side-note, notice guideline 3 says "each option name should be a single alphanumeric character". You can come up with a ton of examples of utilities that use multi-character option names.