Skip to main content

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

*: Save/export to env vars

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 or python3 index.py --help
  • A CLI interface through python3 index.py --username Denperidge and python3 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.

Metadata

Release files for AnyArgs 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AnyArgs 0.1.1
File Size Uploaded Provenance Actions
AnyArgs-0.1.1.tar.gz 10.7 kB Without provenance Details for AnyArgs-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for AnyArgs 0.1.1
File Interpreter ABI Platform Actions
AnyArgs-0.1.1-py3-none-any.whl Python 3 none any Details for AnyArgs-0.1.1-py3-none-any.whl

Total release size: 20.6 kB

Release files / AnyArgs-0.1.1.tar.gz

Download URL AnyArgs-0.1.1.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f2f1be8c1c21daf2bbc7dfc14d3ade41d9a643a808134ae9b80c2e1a93342d51
BLAKE2b-256 checksum
How to use checksums
91221208da7b098a1200dbe03e55db77cfdec14a423c97397f739556a2aa9b1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / AnyArgs-0.1.1-py3-none-any.whl

Download URL AnyArgs-0.1.1-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
242f3de149bc30fec6e2d31c1072b91ef184d041077a4f45558b25cc3a0881e1
BLAKE2b-256 checksum
How to use checksums
d02a5ebed8707aa621bf0f9a184268c984cb85e5b5a04d84c453384122967d40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Security for AnyArgs 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page