Get script arguments from the CLI, .conf files, environment variables and/or .env files, with only one syntax!
CLI | .conf | .env | env vars | Functionality |
---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | Add groups & arguments everywhere at once |
✅ | ✅ | ✅ | ✅ | Load arguments, no matter where they're set |
❌ | ✅ | ✅ | ❕* | Save/export args |
❔ | ❔ | ❔ | ❔ | Set list type/duplicate args |
✅: Implemented ❕: Implemented with caveats ❔: Planned ❌: Not implemented
python3 -m pip install AnyArgs
In just a few lines of code, you can allow your users to set args in whatever way they prefer
# index.py
args = AnyArgs()
args.add_group("Config").add_argument("Username", help="Username for logging in")
args.load_args()
print("Provided username: " + args.get_argument("Username"))
That's it! With just these lines of code, you've allowed the following
- A help interface through
python3 index.py -h
orpython3 index.py --help
- A CLI interface through
python3 index.py --username Denperidge
andpython3 index.py -u Denperidge
- Allow configuring through a .env in the current working directory with
USERNAME=Denperidge
- Allow configuring through environment variables with
export USERNAME=Denperidge
- Allow configuring through a *.conf in the current working directory with
[Config] Username=Denperidge
# index.py
args = AnyArgs()
args.add_group("Config").add_argument("Load on launch", typestring=ARGTYPE_BOOLEAN, help="Whether to load files on launch", default=False)
args.load_args()
And now, a simple python3 index.py --load-on-launch
is enough to enable load-on-launch!
While AnyArgs will auto-generate some flags, you can always define your own instead to override this behaviour!
# index.py
args = AnyArgs()
args.add_group("Config").add_argument("Username", cli_flags=["--username", "--login", "--email"])
args.load_args()
# index.py
args = AnyArgs()
args.add_group("Config").add_argument("Username", help="Username for logging in")
args.load_args()
args.save_to(conf_filepath="conf.conf", env_filepath=".env", env_vars=True) # To only save to one or two of these, simply omit the other values
Output:
.env
# Login
Username=Denperidge
conf.conf
[Login]
username = Denperidge
Env_vars:
print("Env: " + environ.get("Username", None))
# Output:
# Env: Denperidge
git clone https://github.com/Denperidge-Redpencil/AnyArgs.git
cd AnyArgs
python3 -m pip install -r requirements.txt
python3 -m src.tests
git clone https://github.com/Denperidge-Redpencil/AnyArgs.git
cd AnyArgs
python3 -m pip install -r requirements.txt
python3 src.AnyArgs
git clone https://github.com/Denperidge-Redpencil/AnyArgs.git
cd AnyArgs
python3 -m pip install --upgrade build setuptools
python3 -m build && python3 -m pip install --force-reinstall ./dist/*.whl
There are different argtypes. Defining them will change how your arguments get handled & parsed.
Argtype name | Literal value | Behaviour |
---|---|---|
ARGTYPE_STRING |
"STRING" |
View |
ARGTYPE_BOOLEAN |
"BOOL" |
View |
ARGTYPE_LIST |
"LIST" |
View |
Default, will be used when no argtype is defined.
Simple string storage
Not yet implemented.
When cli-flags is undefined while defining arguments, AnyArg will try to auto-generate some. Below are some examples that illustrate how auto-generation works.
args.add_group("Login").add_argument("Username", typestring=ARGTYPE_STRING).add_argument("Handle")
Will result in the following output syntax:
usage: example.py [-h] [--username USERNAME] [--handle HANDLE]
optional arguments:
-h, --help show this help message and exit
Login:
--username USERNAME, -u USERNAME
--handle HANDLE
- Long-flag arg under --NAME
- Short-flag using first letter(s) under -N
- If an auto-generated flag would conflict with another (whether that be from the predefined
--help
/-h
or previously added args), it does not get generated
args.add_group("Save Configuration").add_argument("To .conf", typestring=ARGTYPE_BOOLEAN)
Will result in the following output syntax:
usage: test.py [-h] [--to-conf]
optional arguments:
-h, --help show this help message and exit
Save Configuration:
--to-conf, -tc
- Long-flag arg replaces spaces with dashes (
-
) and ignores non-letters ().
- Short-flag ignores non-letters (
) and uses the first letters split by spaces (.
First of all: apparently on MacOSX & FreeBSD modifying environ is also bad news.
That aside you should note that this - due to limitations of Python (at least without wild workarounds) - is a bit limited. The environment variables should normally get modified for the Python script and its child processes, but not outside of it. This is because Python environ is not persistent outside of the python script runtime.
You can view some explanations and possible workarounds here and the more out of date Python2 discussion here.