Skip to main content

The cliopts package is a Python library for parsing command line arguments. It provides a simpler and more intuitive API with less code and easy cli argument parsing.

Project description

Cliopts Library

PyPI - Python Version

PyPI - Downloads

The Cliopts Library is a Python library designed to simplify the process of parsing command-line arguments. It provides a straightforward and intuitive API, reducing the amount of code required for CLI argument parsing.

Installation

To install the Cliopts Library, run the following command in your terminal (cmd, bash, PowerShell, etc.):

pip install cliopts

Usage

To use the library in your code, follow these steps:

  1. Import the CliArguments class from the cliopts module:

    from cliopts import CliArguments
    
  2. Create an instance of CliArguments and pass a list of argument names or a dictionary of argument names and their shorthand notations, along with optional parameters such as options_desc and version.

    Using a list of options:

    args = CliArguments(
    
        options=["filename", "count", "verbose"],
    
        options_desc={
    
            "filename": "Specify the filename",
    
            "count": "Specify the count",
    
            "verbose": "Enable verbose output"
    
        },
    
        version="v1.0.0"
    
    )
    

    Using a dictionary of options with shorthand notations:

    args = CliArguments(
    
        options={
    
            "filename": "f",
    
            "count": "c",
    
            "verbose": "v"
    
        },
    
        options_desc={
    
            "filename": "Specify the filename",
    
            "count": "Specify the count",
    
            "verbose": "Enable verbose output"
    
        },
    
        version="v1.0.0"
    
    )
    
  3. Access the parsed command-line arguments as a dictionary using the to_dict() method:

    print(args.to_dict())
    

    The to_dict() method returns a dictionary containing the parsed arguments.

  4. Run your Python script and pass command-line arguments using the specified options and their shorthand notations:

    py test.py --filename="filename.txt" --count=5 --verbose=True
    
    py test.py -f "filename.txt" -c 5 -v True
    

    Replace test.py with the name of your script file and filename.txt with the desired value for the argument.

Example

Let's consider an example to illustrate how to use the Cliopts Library. Suppose we are creating a Python script that takes a filename, count, and a verbose flag as input from the command line.

In script.py file:

from cliopts import CliArguments



# Define the desired arguments: filename, count, verbose

args = CliArguments(

    options={

        "filename": "f",

        "count": "c",

        "verbose": "v"

    },

    options_desc={

        "filename": "Specify the filename",

        "count": "Specify the count",

        "verbose": "Enable verbose output"

    },

    version="v1.0.0"

)



print(args.to_dict())

In the command line:

py script.py --filename='/files/filename.txt' --count=5 --verbose=True

py script.py -f '/files/filename.txt' -c 5 -v True

The output of args.to_dict() will be:

{

    "filename": "/files/filename.txt",

    "count": 5,

    "verbose": True

}

CliArgument class params

options

  • Type: Iterable[str] | dict[str, str]

  • Description: A list or dictionary of command-line options. If using a dictionary, the keys are the full option names and the values are their shorthand notations.

options_desc

  • Type: dict[str, str]

  • Optional: Yes

  • Description: A dictionary with descriptions for each option. Defaults to an empty dictionary.

version

  • Type: str

  • Optional: Yes

  • Description: The version of the program. Defaults to None.

help

  • Type: (dict[str, str]) -> Any

  • Optional: Yes

  • Description: A function to display help information. This function takes options_desc as its argument. If not provided, the default help function is triggered utilizing options_desc.

throw_on_invalid_args

  • Type: bool

  • Optional: Yes

  • Description: Whether to throw an error on invalid arguments. Defaults to True.

name

  • Type: str

  • Optional: Yes

  • Description: The name of the program. Defaults to "python-program".

desc

  • Type: str

  • Optional: Yes

  • Description: The description of the program. Defaults to None.

Default Help Function Output

If the --help flag is used, the default help function displays the following information:


Usage: python-program [options]



Options:

    filename : Specify the filename

    count : Specify the count

    verbose : Enable verbose output



--version : Show version

Attach Version

If you want your script to return a version number when prompted with --version, you can easily achieve that by passing a version string as the version parameter when creating an instance of CliArguments.

Example in script.py file:

from cliopts import CliArguments



args = CliArguments(

    options=["filename", "count", "verbose"],

    version="v1.0.0"

)

You can now check the script version using the following command:

py script.py --version

The output will be v1.0.0, matching the version parameter.

Contact the Developer

For any inquiries or assistance, you can contact the developer at ssanmeet123@gmail.com. Feel free to reach out with any questions or feedback you may have.

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

cliopts-1.2.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

cliopts-1.2.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file cliopts-1.2.0.tar.gz.

File metadata

  • Download URL: cliopts-1.2.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for cliopts-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1af03f009a31952d93bae24b100385a51c5177afe1e6427f65933457c13a985e
MD5 bd2ff5382b829a583134595bfdd87f03
BLAKE2b-256 855467dacc7cc928fe95e5dcc3ac3e0b02c8910d79d3fd212476957484b8c5a5

See more details on using hashes here.

File details

Details for the file cliopts-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: cliopts-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for cliopts-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 46526323913ff2bce5550d3cbb267199e61b03bae1bcf98834636d5568a3cdd1
MD5 651199ac96396897a20f24b32219b6e5
BLAKE2b-256 2704879e730d92cc8758571cee4f3239f5aa0936756a273500c8d5309db68d3d

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