Argparse: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
Commandline (gnu-style) arguments in a nutshell: | |||
* Positional arguments are simple (unnamed) arguments, appearing without an "option" first | |||
* Optional arguments are preceded by a "dash" character and name, followed by a value. | |||
* Finally sometimes an option is simply a "flag", or a name with no value, to typically switches some behaviour on or off (like a True/False value). | |||
* There are two forms of options, short and long (see below). | |||
Nothing is set in stone, a command can choose to interpret arguments in any way it sees fit. But many programs follow the GNU standard of options for consistency. | |||
== Short and Long options == | |||
Short form means that the option name is simply a single letter preceded by a single dash, and then followed immediately (no space in between) by a value. Example of a short-form option in the "head" command: | |||
ls | head -n1 | |||
Long form means a longer name is preceded by two dashes and then followed by either an equals sign or a space, and finally the value. An example of a long-form option of again the command "head": | |||
ls | head --lines=1 | |||
Or the equal sign can be left off | |||
ls | head --lines 1 | |||
== import argparse== | |||
In Python, using the argparse (formerly optparse) module is an easy way to create commandline programs that are flexible, and behave like other "big" programs. When using argparse, options can appear in any order, before or after positional arguments. Argparse also automatically produces a "help" screen for explaining (reminding) how a script can be used. | In Python, using the argparse (formerly optparse) module is an easy way to create commandline programs that are flexible, and behave like other "big" programs. When using argparse, options can appear in any order, before or after positional arguments. Argparse also automatically produces a "help" screen for explaining (reminding) how a script can be used. | ||
<source lang="python"> | |||
import argparse | |||
parser = argparse.ArgumentParser(description="My first Inkscape effect") | |||
parser.add_argument('--id', action="append", help="id(s) of selected elements") | |||
parser.add_argument('path', help="path of svg file to open") | |||
args = parser.parse_args() | |||
print args | |||
</source> | |||
* If the name starts with "--" it's considered an "option", otherwise it's "positional" | |||
* Action = "append" for a list of values (allows zero or more values to be provided) | |||
* Default = "foo": To set a default value | |||
== Resources == | == Resources == | ||
http://docs.python.org/library/argparse.html#module-argparse | * http://docs.python.org/library/argparse.html#module-argparse | ||
[[Category: Cookbook]] [[Category: Python]] |
Latest revision as of 13:41, 26 October 2011
Commandline (gnu-style) arguments in a nutshell:
- Positional arguments are simple (unnamed) arguments, appearing without an "option" first
- Optional arguments are preceded by a "dash" character and name, followed by a value.
- Finally sometimes an option is simply a "flag", or a name with no value, to typically switches some behaviour on or off (like a True/False value).
- There are two forms of options, short and long (see below).
Nothing is set in stone, a command can choose to interpret arguments in any way it sees fit. But many programs follow the GNU standard of options for consistency.
Short and Long options
Short form means that the option name is simply a single letter preceded by a single dash, and then followed immediately (no space in between) by a value. Example of a short-form option in the "head" command:
ls | head -n1
Long form means a longer name is preceded by two dashes and then followed by either an equals sign or a space, and finally the value. An example of a long-form option of again the command "head":
ls | head --lines=1
Or the equal sign can be left off
ls | head --lines 1
import argparse
In Python, using the argparse (formerly optparse) module is an easy way to create commandline programs that are flexible, and behave like other "big" programs. When using argparse, options can appear in any order, before or after positional arguments. Argparse also automatically produces a "help" screen for explaining (reminding) how a script can be used.
import argparse
parser = argparse.ArgumentParser(description="My first Inkscape effect")
parser.add_argument('--id', action="append", help="id(s) of selected elements")
parser.add_argument('path', help="path of svg file to open")
args = parser.parse_args()
print args
- If the name starts with "--" it's considered an "option", otherwise it's "positional"
- Action = "append" for a list of values (allows zero or more values to be provided)
- Default = "foo": To set a default value