Tuesday, May 7, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2137  / 2 Years ago, sat, october 8, 2022, 1:24:24

I have noticed that some programs work differently when the flags are used before the input parameters, rather than adding the flags at the end. So I want to know, what is the conventionally accepted way to order parameters and flags in an Ubuntu CLI program.



I'm specifically asking about Ubuntu as this is my platform of concern. I understand that this depends on the program being run, but what is the norm?



Eg:



./myprog -d file.txt


Vs



./myprog file.txt -d

More From » command-line

 Answers
4

There is no correct order, as it varies from program to program. The OS just hands the command line arguments over to the program in the order they are given. The way in which they are parsed depends on the program or the parsing libraries that are used.



In most cases the order doesn't matter and common parsing libraries like getopt or Python's argparse allow order independent parsing. But other programs can be more picky. Also note that even with order independent parsing you still have order depended arguments. Meaning some options must be follow by a argument:



 ls --sort time -l


The time here is an argument to the --sort option and thus must come after it. However the order of --sort time and -l doesn't matter. Many programs allow writing --sort=time to make this more explicit, but not all do.



The GNU project does have a coding standard for command line handling and most of their tools follow that, but it's not something you can depend on.



In cases where order does matter, you generally do ./myprog -d file.txt and it looks nicer in shell scripts as well. The ./myprog file.txt -d style of writing is more for the command line when you just typed the thing and want to add a -d flag, but not cursor all the way back to the middle of the line.


[#15941] Saturday, October 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
splenueak

Total Points: 448
Total Questions: 118
Total Answers: 110

Location: Vanuatu
Member since Mon, Oct 3, 2022
2 Years ago
;