Reputation: 5205
If not, is there a de facto standard? Basically I'm writing a command line help text like so:
usage: app_name [options] required_input required_input2
options:
-a, --argument Does something
-b required Does something with "required"
-c, --command required Something else
-d [optlistitem1 optlistitem 2 ... ] Something with list
I made that from basically just reading the help text of various tools, but is there a list of guidelines or something? For example, do I use square brackets or parentheses? How to use spacing? What if the argument is a list?
Upvotes: 385
Views: 202696
Reputation: 17597
I think there is no standard syntax for command line usage, but many use this convention:
Microsoft Command-Line Syntax (IBM also had similar syntax, but the link is no more valid):
text without brackets or braces
for items you must type
Ex: ls
<placeholder for a value>
Ex: cat <file>
, <file>
should be replace by the path of the file
[optional]
Ex: ls [-l]
can be ls
or ls -l
{a|b}
for mutually exclusive items
Ex: ip {link|addr}
can be ip link
or ip addr
<file> ...
for items can be repeated
Ex: cat <file> ...
can be cat a.txt b.txt c.txt
Upvotes: 114
Reputation: 1175
I use the CSS formal notation for this.
Component values may be arranged into property values as follows:
- Several juxtaposed words mean that all of them must occur, in the given order.
- A bar (
|
) separates two or more alternatives: exactly one of them must occur.- A double bar (
||
) separates two or more options: one or more of them must occur, in any order.- A double ampersand (
&&
) separates two or more components, all of which must occur, in any order.- Brackets (
[ ]
) are for grouping.Juxtaposition is stronger than the double ampersand, the double ampersand is stronger than the double bar, and the double bar is stronger than the bar. Thus, the following lines are equivalent:
a b | c || d && e f [ a b ] | [ c || [ d && [ e f ]]]
Every type, keyword, or bracketed group may be followed by one of the following modifiers:
- An asterisk (
*
) indicates that the preceding type, word, or group occurs zero or more times.- A plus (
+
) indicates that the preceding type, word, or group occurs one or more times.- A question mark (
?
) indicates that the preceding type, word, or group is optional.- A pair of numbers in curly braces (
{A,B}
) indicates that the preceding type, word, or group occurs at least A and at most B times.
If you need examples, see Formal definition sections on MDN; here is one for font
: https://developer.mozilla.org/en-US/docs/Web/CSS/font#formal_syntax.
And here is a simple example from my own Pandoc's cheat sheet:
$ pandoc <input_file>.md --from [markdown|commonmark_x][-smart]? --to html --standalone --table-of-contents? --number-sections? [--css <style_sheet>.css]? --output <output_file>.html
Upvotes: 0
Reputation: 301
It may be a bit off-topic, but I once wrote two small tools that make creation and maintenance of command line tools help pages more efficient:
I integrate these two tools in the MAVEN build process of my programs so they execute automatically on every build.
For example:
--help
command line optionHope this is useful for others!?
Upvotes: 1
Reputation: 739
We are running Linux, a mostly POSIX-compliant OS. POSIX standards it should be: Utility Argument Syntax.
-o
. -o argument
or
-oargument
. -lst
is equivalent to -t -l -s
.-lst
is equivalent to -tls
.-lst
nonoption.--
argument terminates options.-
option is typically used to represent one of the standard input
streams.Upvotes: 33
Reputation: 24831
Typically, your help output should include:
[options]
to indicate where the options goarg_name
for a required, singular arg[arg_name]
for an optional, singular argarg_name...
for a required arg of which there can be many (this is rare)[arg_name...]
for an arg for which any number can be suppliedarg_name
should be a descriptive, short name, in lower, snake case-l
) or a long form (e.g. --list
), include them together on the same line, as their descriptions will be the sameGREP_OPTS
Note further that it's good form to accept both -h
and --help
to trigger this message and that you should show this message if the user messes up the command-line syntax, e.g. omits a required argument.
Upvotes: 249
Reputation: 3017
Take a look at docopt. It is a formal standard for documenting (and automatically parsing) command line arguments.
For example...
Usage:
my_program command --option <argument>
my_program [<optional-argument>]
my_program --another-option=<with-argument>
my_program (--either-that-option | <or-this-argument>)
my_program <repeating-argument> <repeating-argument>...
Upvotes: 153
Reputation: 8337
Microsoft has their own Command Line Standard specification:
This document is focused at developers of command line utilities. Collectively, our goal is to present a consistent, composable command line user experience. Achieving that allows a user to learn a core set of concepts (syntax, naming, behaviors, etc) and then be able to translate that knowledge into working with a large set of commands. Those commands should be able to output standardized streams of data in a standardized format to allow easy composition without the burden of parsing streams of output text. This document is written to be independent of any specific implementation of a shell, set of utilities or command creation technologies; however, Appendix J - Using Windows Powershell to implement the Microsoft Command Line Standard shows how using Windows PowerShell will provide implementation of many of these guidelines for free.
Upvotes: 11
Reputation: 37258
yes, you're on the right track.
yes, square brackets are the usual indicator for optional items.
Typically, as you have sketched out, there is a commandline summary at the top, followed by details, ideally with samples for each option. (Your example shows lines in between each option description, but I assume that is an editing issue, and that your real program outputs indented option listings with no blank lines in between. This would be the standard to follow in any case.)
A newer trend, (maybe there is a POSIX specification that addresses this?), is the elimination of the man page system for documentation, and including all information that would be in a manpage as part of the program --help
output. This extra will include longer descriptions, concepts explained, usage samples, known limitations and bugs, how to report a bug, and possibly a 'see also' section for related commands.
I hope this helps.
Upvotes: 1
Reputation: 59811
The GNU Coding Standard is a good reference for things like this. This section deals with the output of --help
. In this case it is not very specific. You probably can't go wrong with printing a table showing the short and long options and a succinct description. Try to get the spacing between all arguments right for readability. You probably want to provide a man
page (and possibly an info
manual) for your tool to provide a more elaborate explanation.
Upvotes: 12