Usage
Declaring flags
Declare flags with the global parser instance:
import arglite
arglite.parser.require("name", type=str)
arglite.parser.optional("count", default=1, type=int)
arglite.parser.flag("verbose")
| Method | Purpose |
|---|---|
parser.require(name, type=None) |
Required flag. Exits with an error if missing. |
parser.optional(name, default=None, type=None) |
Optional flag with a default value. |
parser.flag(name) |
Boolean flag. True if present, otherwise False. |
Accessing values
Access declared flags as attributes on the parser. The first time you access a flag, arglite parses sys.argv.
Short flags
If you do not provide a short alias, the first letter of the flag name is used automatically.
All of these work:
You can also set an explicit short alias:
Types
arglite converts values automatically.
arglite.parser.require("count", type=int)
arglite.parser.require("ratio", type=float)
arglite.parser.optional("debug", default=False, type=bool)
Boolean flags are the easiest way to get a simple on/off switch:
--verbose→True- absent →
False --verbose=false→False
Edge cases
Values with spaces
Use normal shell quoting:
Empty strings
Empty strings are preserved:
Negative numbers
Values that start with - are handled correctly when they follow a value-expecting flag:
Errors
arglite exits with code 1 and a clear message when something is wrong: