Skip to main content

Argument aliases/abbrevations for CLI programs (compatible with argparse/click/typer/...)

Project description

ArgAlias

Run tests and upload coverage codecov

Aliases for arguments in Python CLI utilities (supports argparse, Typer, Click, and similar tools).

This tool is for those who agree that p sh X is shorter then project show X.

You configure which canonical CLI arguments you expect, and what aliases are allowed (e.g. for the canonical argument "show" good aliases are "sh" and "get").

If a canonical name is configured with a prefix, aliases will be resolved only if they follow configured prefix from the first argument.

Add aliases to your argparse, Click, or Typer project in 10 minutes!

Installation

pip3 install argalias

How to use

When you add aliases, the first element is either a str (the canonical name to which the alias will be resolved to) or a List[str] containing prefixes with canonical name.

Each element in the prefix list can be either a simple string (a parameter), * (matches any value of parameter) or values separated by the | symbol.

from argalias import ArgAlias

aa = ArgAlias()

# The script expects "show" parameter anywhere, and it can be aliased as "sh", "s" or even "get"
aa.alias("show", "get", "sh", "s")

# The script expects "employee" as the first parameter, can be aliased as "emp" or "e" 
aa.alias(["employee"], "emp", "e")

# same for "project"
aa.alias(["project"], "proj", "p")

# The script expects "create" parameter after "employee" or "project". Can be aliased as "cr" or "c"
aa.alias(["employee|project", "create"], "cr", "c")

# The script expects "delete" as the second parameter after any parameter, can be aliased as "del" or "d"
aa.alias(["*", "delete"], "del", "d")
aa.parse()
# sys.argv now has all aliases resolved, e.g. "sh" resolved to "show"
print(sys.argv)

# now all aliases in sys.argv are resolved and you can do your argparse or click or typer parsing

You can find examples using argparse, Click, and Typer in examples/.

Results:

p sh Something -> project show Something
project cr Mayhem -> project create Mayhem

These aliases will not be replaced:

sh emp John ("sh" resolved to "show" but "emp" will not be resolved: not a first argument)
zzz cr xxx ("cr" will not be resolved: not after employee or project)
aaa bbb del ("del" will not be resolved: prefix is "aaa", "bbb" - two elements, but "*" matches only one element)

Might be replaced incorrectly:

project create sh (You want to create a project named "sh", but "sh" will be replaced with "show" because the alias does not specify any prefix requirements.)

Optional arguments

Optional arguments can make a problems while checking prefixes, e.g. script.py -v p del X will not match ["project"] prefix, because first argument is "-v", not "p". Here skip_flags() and nargs() comes to help.

# with skip_flags ArgAlias will ignore any unknown arguments starting with "-", e.g. "-v", or  "--some-option"
aa.skip_flags()

# this will ignore --xy 11 22 
aa.nargs('--xy', nargs=2)

# this will ginore --level 123 (default value for nargs is 1)
aa.nargs('--level')

See argparse_ex1.py for a real example. You do not need to use skip_flags or nargs:

  1. If you replace parameter anywhere in args (like "show" in example above), not using prefix.
  2. If options will go after alias (e.g. script.py project del X --verbose)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

argalias-0.1.4.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argalias-0.1.4-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file argalias-0.1.4.tar.gz.

File metadata

  • Download URL: argalias-0.1.4.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for argalias-0.1.4.tar.gz
Algorithm Hash digest
SHA256 dfbb2f32e6a2af6d69ce6d6350d599c298bbc14d1ea1feaaa4bfe2e4c0cea714
MD5 9e4b0dc0f69e82f7b78d84659594a26e
BLAKE2b-256 0e8981206496444957d4b44cf22adefad19eaafd1573d79c3326aa16d9f8df5e

See more details on using hashes here.

File details

Details for the file argalias-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: argalias-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for argalias-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7fc76c730642e6df050348f18b07026913f3f28ca3eafbc6068d37bd436f0cbf
MD5 2f91f9f5afc6d1f57f646f6b7b9d23cb
BLAKE2b-256 787b25f781210442f39d10ae2389a33f1d0d052eebabcd0e51744ec877da94fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page