Sanmeet007 / cliopts

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.

Home Page:https://pypi.org/project/cliopts/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

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.

https://pypi.org/project/cliopts/

License:GNU General Public License v3.0


Languages

Language:Python 100.0%