baseopt
A simple package for parsing and managing CLI options/arguments from getopt.
Install
pip install baseopt
Usage
- Use
BaseOptions
to create an option list - Add individual
BaseOption
s to the BaseOptions
object - Use
[]
of BaseOptions
to get a BaseOption
object - Use
()
of BaseOptions
to get the value of a BaseOption
object
import sys
from baseopt import BaseOption, BaseOptions
options = BaseOptions([
BaseOption(name="help", shortname="h", dtype=bool, default=False),
BaseOption(name="file", shortname="f", dtype=str, doc="Path to the input file")
])
class Options(BaseOptions):
def __init__(self):
super().__init__()
self.add(name="help", shortname="h", dtype=bool, default=False)
self.add(name="file", shortname="f", dtype=str, doc="Path to the input file")
options = Options()
options.parse(sys.argv[1:])
if options("help"):
options.help()
sys.exit(1)
print(options("file"))
Executing the above script gives
Options:
-h | --help
-f | --file Path to the input file
(def = "None")