LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2011, 05:30 AM   #1
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
standard for usage statement syntax


Hi. I'm writing some small command-line programs. Is there some POSIX or GNU standard document somewhere describing what the usage statement syntax is supposed to be?; specifically I am referring to part of the usage statement that describes the command syntax. For example, what symbol should be used to show that an argument is optional (parenthesis, square brackets, angle brackets...); should argument names be capitalized?; how to show if one of two options is mandatory?; and so forth.

I found a GNU coding standards page, but this information doesn't seem to be on it.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-25-2011, 06:45 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
First, make sure you use getopt() or the same rules (self-implemented) to parse the options. Getopt() is standardized in IEEE Std 1003.1-2001 (also known as SUSv3, and includes POSIX, also known as IEEE Std 1003.2).

SUSv3/IEEE Std 1003.1 includes a chapter on Utility Conventions, including Utility Argument Syntax and Utility Syntax Guidelines.

The Utility Argument Syntax section describes the format used in IEEE Std 1003.1 exactly, and is used by practically all Linux/POSIX utilities, including the synopses in man pages.

man man and man getopt manpages list the format quite concisely. Here is an abbreviated and simplified list of examples:

Code:
-abc                    short options -a, -b, -c
[-a]                    optional short option
[-abc]                  none, any, or all of optional short options -a, -b, -c
[-d parameter]          short option with a required parameter
argument ...            one or more arguments
[argument] ...          zero or more arguments
-h|--help               alternatives (here, short and long form of the same parameter)
-i|-o|-c                alternatives (one of -i, -o, or -c must be used)
[-i|-o|-c]              alternatives (one of -i, -o, or -c may be used)
[-i file|-o file] ...   short option -i and -o both need a parameter, but can be repeated
--long=value            long option with a parameter
--long value            long option with a parameter
--long[=value]          long option with an optional parameter
Sometimes you see <input file> ... syntax in usages; I've used it to try to make the usage a bit more intuitive. (This derives from BNF syntax used in RFC's, and has nothing to do with the standards and conventions listed above.)

Sometimes you can see extra spaces around [ | ]. These are for readability only, and are strictly not according to the standards and conventions listed above. The main thing is to keep the surprises for the user to a minimum. I don't think small deviations from the rules are significant. I like to use a blind test: Give the script or utility to someone who uses Linux command line tools, and see if they need more than a couple of seconds to look at the usage before correctly using the tool. If they are happy and fast with the tool, I'm happy. If not, it's usually time to edit the options. (Many short options, like -v and -f, are used the same way in a lot of utilities. If more than one tester uses the wrong option, it is usually better to switch the option letters around.)

Last edited by Nominal Animal; 12-25-2011 at 06:58 AM.
 
3 members found this post helpful.
Old 12-25-2011, 07:34 PM   #3
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467

Original Poster
Blog Entries: 60

Rep: Reputation: 51
Thank you Nominal for a helpful and detailed reply.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] script help (Case & IF statement syntax), easy to answer if you know shell scripting. casperpache Linux - Newbie 12 05-09-2011 07:18 AM
[SOLVED] Bash: if statement syntax ghantauke Linux - Newbie 3 12-03-2010 09:04 AM
gfortran: Syntax error in DATA statement at (1) science_guy Programming 1 10-16-2009 12:10 AM
[Python] Invalid Syntax - If statement LinuxCrayon Programming 4 12-07-2007 09:11 AM
htdig.conf start url: statement syntax for local indexing. AwesomeMachine Linux - Software 0 05-02-2006 02:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration