356 points · by spyh · 5y ago · 246 comments
(https://golang.org/pkg/flag/#hdr-Command_line_flag_syntax)
Edit: To be clear, I'm mostly "blaming" Go for re-popularizing this style by a) putting it in the standard library and b) being a widely used programming language; I'm not saying Go came up with this or anything.
(idk about the space separated args tho that's even worse)
Not sure about the relevant point on compact short options syntax as in `tar -xvzf archive.tgz` though... (edit) after a quick & sloppy test it seems to work as expected
The main design motivation of absl::Flags is that the flag definitions can appear in any module, not just main. Go inherits this. A quirk that Go did not inherit is gflags --nofoo alternate form of --foo=false.
This is all documented at https://gflags.github.io/gflags/#commandline, which is pretty much a verbatim export of the flags package documentation that a Google engineer would see internally.
It's a convention-over-configuration thing I think. I mean they set a standard, so you can move on. The alternative is to sit and think and discuss about what order to put your documentation in.
Fortunately, there are alternative packages that does.
However, even those seem to raise some questions, for example:
> To make command line even more confusing, multiple tools I have used allow spaces in optional arguments (e.g. "-opt1 arg1 arg2 -opt2", where arg1 and arg2 set two values for -opt1).
Is described as something that's permissible:
> An option and its argument may or may not appear as separate tokens. (In other words, the whitespace separating them is optional.) Thus, ‘-o foo’ and ‘-ofoo’ are equivalent.
Therefore the below would be considered equivalent:
-opt1 arg1 arg2 -opt2
-opt1arg1arg2 -opt2
Were your expectations different?Are there any good articles on the benefits of following such rules (any fungible improvements to legibility or usability, as opposed to just "consistency amongst different tools")?
Are there any tools which can validate whether any piece of software conforms to this standard (either by scanning the man pages, or the code, or a formalized format of parameters the app supports)? Personally, the closest i've found is Typer ( https://typer.tiangolo.com/ ) but without anything that can automatically reject non-conformant code as a part of a CI process, i think enforcing such formats would be a non-starter for me.
foreach a in arglist
if a=~/^-(\w+)(?:=(.+))?/
$opts{$1} = $2;
else
push @pos_list, $a
There is no need for the dependency and complexity of `getopt` library.And for the user side, no more cryptic ninja arts. The only trick user need to learn is the shell alias and functions.
In your regex at least, removing the confusion is simple as adding another ‘-‘, and now you’re in compliance with the expectations of almost every IT person in the world who uses Unix command lines.
https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html
If it mattered much, I'd expect GNU to be internally consistent.
GNU's conventions are generally complimentary, but not incompatible with POSIX. And POSIX specifies the behavior of the sort of flags cc(1) should understand[1].
There are many POSIX and other traditional *nix tools that are a convention unto themselves for historical reasons. E.g. notice how GNU "dd" doesn't follow normal GNU command-line conventions either.
1. https://pubs.opengroup.org/onlinepubs/7908799/xcu/cc.html
The obviously right choice would have been [ ]. You know, like in
if [ $foo ... ]find is annoying, though. I'd encourage you to check out fd.
vs. Unix where char *argv[] is what makes it to the syscall layer.
The result there is that command line processing is more consistent program-to-program on Unix. On Windows, every program could decide to tokenize the arguments differently.
Worse, even Microsofts two implementations (CRT and WINAPI) disagree: https://github.com/rust-lang/rust/issues/44650
I had the impression only CLI tools from the Java world are that strange.
I’m also glad short opts are available for my personal day-to-day work. I spend most of my time in a a terminal and appreciate having short-hand available.
test -f foo -a -f bar -o -z foo
can be read isfile('foo') && isfile('bar') || emptystring('foo')
Likewise find . -name '*.py' -a -executable -o -printf '%P\n'
can be read (F.name matches '*.py') && (F is executable) || print('%P\n', F)
where F is the current node in the file system traversal.They both respect -o as OR, ! as NOT, and ( ) for precedence, which you have to quote as \( and \).
A couple years ago, someone helped me implement a better "find" without this wonky syntax for https://www.oilshell.org/ . But it isn't done and needs some love. If anyone wants to help, feel free to join Zulip :)
I do think that "find" is more like a language than a command line tool. It's pretty powerful, e.g. I just used it to sort through 20 years of haphazard personal backups.
$ find -name something
find: illegal option -- n
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
What does "illegal option" mean exactly? Why is it "n" which is the first letter of "-name"? Yes, it wants a path. Yes, even if you want to search in the current directory. Yes, it IS unusual, because all other commands that operate on directories, like `ls`, assume current directory if you don't specify any.Why could it not just say "a path is required" instead?
tar xvf foo.tarI've seen that pattern before, but it always drives me a little crazy.
Alternatively, it could offer a y/n prompt.
Because if you don't care about chaining together short flags and just want to use two dashes for your long flags, Go will happily accept that.
https://golang.org/pkg/flag/ : "One or two minus signs may be used; they are equivalent."
You mean around 2002-2006? I find that pretty hard to believe.
Early versions of MS-DOS made it a user preference option in the command.com interpreter, whether the user wanted to use / for options and \ for path separation or vice versa.
That's what happens I guess when the people designing it haven't actually used a CLI day to day much, because, well, they're using Windows.
Seeing functions aliased to their POSIX names is already a little bit misleading when you realize they are not a drop-in replacement at all.
Because they really didn’t.
Usage: pr [+page] [-columns] [-h header] [-w with] [-l length] [-nt] [files]
Mixed in this case, with -columns and +page, and all hand parsed. But long options nonetheless.to format for printing starting at page 4, on a wide 172 colunn paper?
find dir -type f -name "*.h" -print find <options> <paths> <expression>
Those thing you list are part of the <expression> part of the command. The <options> part in BSD find, and I believe GNU find, only uses options of the form -X where X is a single character.It's a little confusing because the man pages for both BSD and GNU find do call some of the things that appear in the <expression> part of the command "options".
> There were a few programs that ran on Unix systems and used long option names starting with either - or no prefix at all, such as find, but those syntaxes were not compatible with Unix getopt() and were parsed by ad-hoc code.
This was likely before the effects of Eternal September began destroying the public Usenet, so the vote may well have been held there, in one of the newsgroups relevant to the FSF, GCC or GNU.
The '--' alternative won overwhelmingly, as I rememeber it. A few hundred votes were cast by email.
things like this are how I know that there are too many people who are absolutely insane and who make mundane decisions.
- to add
+ to subtract
absolute genius. is this what higher education teaches people? I didn't go to college, and maybe that was best. %p locale's equivalent of either AM or PM
%P like %p, but lower caseIE a line to indicate use of this option and a line with a strikethrough to indicate it has been "struck out" as an option.
But using the conventional minus and plus symbols is indeed confusing.
Presumably it was rejected because the whole point of POSIX was to consolidate, regularize, and simplify pre-existing practice. Adding "+" as an additional standard option signifier would take a huge step in the complete opposite direction. The only precedence for "+" would have been the `set` shell builtin, and AFAIU the committee only begrudgingly grandfathered that syntax.
Someone elsethread mentioned the `date` utility, but if you look at the BSD implementation "+" isn't used as an option marker, per se, but rather to disambiguate operand strings. The 2001 standard only defined the following:
date [−u] [+format]
date [−u] mmddhhmm[[cc]yy]
It's splitting hairs, but POSIX was at least able to shoehorn the legacy syntax into a more regularized base interface. mytool infile=foo.fits outfile=bar.fits
Some parameters need to be given (and will be prompted for if not given), and some get a default if they are omitted. The extra tricky part is that the parameters and their defaults are read from .par files in a path. There are the default ones for the tool, and a user-specific parameter file which can be modified using the "pset" program (or read with "pget"). A tool when run will also modify the par file to update various parameters with the ones given on the command line.Unfortunately, there is no form of locking on these par files, so one has to mess around with the path settings (to make per-process paths), or use some form of locking, to ensure they don't get corrupted if multiple processes are run at once.
EDIT: Trying to remember examples of it; here's one I can recall: http://www.nynaeve.net/?p=180
It’s hard to imagine that the philosophy of these early heroes has so pervaded the world. Free software is everywhere. Other fields don’t do this to the degree software does. So much value for humanity just because the first few took one approach when another would have worked just as well.
sudo --with-wheel-group
has not (yet) been sneaked in to any major *nix distribution. `grep bar -w -- -foo-file`https://pubs.opengroup.org/onlinepubs/009696799/functions/ge...
but no 'getopt_long' (AFAICT).
OpenBSD, just as one example of a true unix-derived system, added it's own getopt_long only in 3.3 release which was 2003, ~17 years after the 1st posix in 1988. This article mentions getopt_long originating ~1990, after 1st posix standard.
date +'%Y-%m-%d-%H-%M-%S'Your example is a format string. date needed to tell if the positional argument is a format or a new date, and to make it easy, they decided to prefix the format with a special character. I am going to guess that this character should not be - (to avoid confusion with options), or numeric (to avoid confusion with new date), not have special meaning in common shells (to keep quoting simpler). They could have chosen : or ^ for example.
Not sure what you are disagreeing with though, I made no claims that someone had to use +, or positional argument in general. Just an observation about what might have guided the design process.
ls -la == ls -l -a, but then you can't ls -all because it's the same, you need --all (or +all).
Java supports -cp or -classpath but in both cases the token after the - always identifies a single option, so you don't need the second dash.
Maybe because java was intended to run on multiple operating systems, although both look out of place on MS-DOS!