Turn all the markdown files in your repos into one big, divio structrured documentation
Project description
AnyArgs
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
How-To
(Project) Install from pip
python3 -m pip install AnyArgs
(Code) Basic usage
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
(Code) Booleans
# 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!
(Code) Explicitly defining flags
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()
(Code) Exporting/saving 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
(Project) Clone & run tests
git clone https://github.com/Denperidge-Redpencil/AnyArgs.git
cd AnyArgs
python3 -m pip install -r requirements.txt
python3 -m src.tests
(Project) Clone & run scripts locally
git clone https://github.com/Denperidge-Redpencil/AnyArgs.git
cd AnyArgs
python3 -m pip install -r requirements.txt
python3 src.AnyArgs
(Project) Clone, build & install package locally
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
Reference
Argtypes
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 |
ARGTYPE_STRING
Default, will be used when no argtype is defined.
Simple string storage
Resulting CLI:
ARGYPE_BOOLEAN
ARGTYPE_LIST
Not yet implemented.
Cli-flag auto-generation
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 (.
Discussions
Save to env vars
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file AnyArgs-0.1.1.tar.gz
.
File metadata
- Download URL: AnyArgs-0.1.1.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2f1be8c1c21daf2bbc7dfc14d3ade41d9a643a808134ae9b80c2e1a93342d51 |
|
MD5 | f897c4bd6ca17b22edf2cd71853980ab |
|
BLAKE2b-256 | 91221208da7b098a1200dbe03e55db77cfdec14a423c97397f739556a2aa9b1f |
File details
Details for the file AnyArgs-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: AnyArgs-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 242f3de149bc30fec6e2d31c1072b91ef184d041077a4f45558b25cc3a0881e1 |
|
MD5 | 09c0efa87c63130dcaf3471acc42be56 |
|
BLAKE2b-256 | d02a5ebed8707aa621bf0f9a184268c984cb85e5b5a04d84c453384122967d40 |