Skip to main content

Make your script args easier

Project description

Args Parser Readme

Overview

This is a simple Python script for parsing command-line arguments and executing corresponding functions. The Args class provides decorators to associate functions with argument names and allows the script to execute the corresponding function when the respective argument is provided in the command line.

Usage

Importing the Script

To use the Args class, you need to import the script into your Python code. Make sure the script file is in the same directory as your Python file or in a location where it can be imported.

from args_parser import Args

Creating an Instance

To get started, create an instance of the Args class:

args_parser = Args()

Associating Functions with Arguments

Use the arg decorator to associate functions with argument names. The arg decorator takes a list of argument names as its parameter.

@args_parser.arg(["-hello", "--greet"])
def greet_user(name):
    print(f"Hello, {name}!")

@args_parser.arg(["-bye", "--farewell"])
def say_goodbye(name):
    print(f"Goodbye, {name}!")

In this example, the greet_user function is associated with arguments -hello and --greet, and the say_goodbye function is associated with arguments -bye and --farewell.

Handling No Arguments

Use the haveArgs decorator to set a function that will be executed when no arguments are provided in the command line.

@args_parser.haveArgs
def no_args_function():
    print("No arguments provided.")

Parsing Arguments

After defining your functions and associating them with argument names, call the parse_args() method of the Args instance to process the command-line arguments and execute the corresponding function.

if __name__ == "__main__":
    args_parser.parse_args()

Running the Script

Run your Python script from the command line, passing the desired argument and its value (if required):

python your_script.py -hello Alice

Output:

Hello, Alice!
python your_script.py -bye Bob

Output:

Goodbye, Bob!
python your_script.py

Output:

No arguments provided.

Conclusion

The Args class simplifies the process of parsing and handling command-line arguments in Python. By using decorators to associate functions with arguments, you can easily expand the functionality of your script and provide a better command-line interface for your users.

Please note that this is a basic example and may require additional error handling and input validation depending on the complexity of your use case. Enjoy using the Args parser and happy coding!

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

argify-0.0.1.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

argify-0.0.1-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file argify-0.0.1.tar.gz.

File metadata

  • Download URL: argify-0.0.1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for argify-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6e2b8686eb39c59a0b5627ca8092d2ba2bb1f196ce62055d9008ede3c065aeec
MD5 fd8aca38e549a9c2371be884f95ca4b8
BLAKE2b-256 a295d1de79ba08f898b82aa2c509748368393567a33ee1fc60403b2b72bce73f

See more details on using hashes here.

File details

Details for the file argify-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: argify-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for argify-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 150511d46e48e46cfde7777b87076ee87ca6a44103ea29f338f518efef29f6b5
MD5 f9b2cb8ec7b70da01069aeb752c0cdff
BLAKE2b-256 2da96de981333f677c3fdeeb5055d4f3a90086748ee9dfc51a6b0bd9beacdd39

See more details on using hashes here.

Supported by

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