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

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 as a parameter:

    args = CliArguments(["options"])
    
  3. Access the parsed command line arguments as a dictionary using the todict() method:

    print(args.todict())
    

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

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

    py test.py --filename="filename"
    
    py test.py -f filename
    
    py test.py --f="filename"
    

    Replace test.py with the name of your script file and filename 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 as input from the command line.

In script.py file:

from cliopts import CliArguments



# Define the desired arguments: filepath, count, boolean

args = CliArguments(["filepath", "count", "boolean"]) 



print(args.todict())

In the command line:

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

The output of args.todict() will be:

{

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

    "count" : 5,

    "boolean": True

}

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 second parameter when creating an instance of CliArguments.

Example in script.py file:

from cliopts import CliArguments



args = CliArguments(["options"], "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 second parameter.

Attach Help Function

If you want your script to run a help program when prompted with --help, you can easily achieve that by passing a help_function , callable as the third parameter when creating an instance of CliArguments.

Example in script.py file:

from cliopts import CliArguments



def help_function(): 

   print("HELP STRING")



args = CliArguments(["options"], "v1.0.0"  , help_function)

You can now run a help function using the following command:

py script.py --help

The output will be HELP STRING, matching the output of function passed as third 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.0.1.tar.gz (15.7 kB view hashes)

Uploaded Source

Built Distribution

cliopts-1.0.1-py3-none-any.whl (16.6 kB view hashes)

Uploaded Python 3

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