Project description

It helps to build a command line tool with argparse module.It also use ConfigParser module to store default values into aconfiguration file store in your profile.

It checks the data type, if it is required, etc... without repeat yourconstraint once for the config file, and another time for the cli parser.Usefull trick, data store in the configuration file can be used as thedefault value of the argparse argument.

This lets you focus on the command you want to do, not the input processing(cli or config file)

Every program build with this tool have auto complete support on options andarguments through argcomplete module.

Installation------------

You can install this module using : ``pip install argtoolbox``.

QuickStart : Very Basic Usage-----------------------------

1. Imports :~~~~~~~~~~~~

First of all, you just need the following classes to build your own script :

* **DefaultCommand :** The default class to extend in order to create your own command class.

* **BasicProgram :** The most simple program to run your command classes.

2. Declaration :~~~~~~~~~~~~~~~~

There is a script called sample-program.py which contains all the followinglines of code.

1. First you have to create your command class TestCommand. (**Step 1**)2. In the ``__call__`` method, you can do every thing you want. The first and only arg of this method is the args object created by Argparse.parser. (**Step 2**)3. You create an other class MyProgram (which extends the BasicProgram) (**Step 3**)4. Now you create your ``argparse.parser`` and your declare your argument, option and command. (**Step 4**)5. Finally you just have to instanciate your class MyProgram and run it. (**Step 5**).

This is the beginning of the TestCommand class. The command line arguments (argv) : ----------------------------------- Namespace(__func__=<__main__.TestCommand object at 0xb721a92c>, config_file=None, host='127.0.0.1', port=3000, verbose=False)

This is the end of the TestCommand class.

You can see the variable ``host`` contains the input message ``127.0.0.1`` into theargs object.The option ``port`` contains the default value ``3000``.

Advanced usage--------------

At this point, this program does not do much more than the argparse module cando.In the cas you have a lot of command and option, it could be usefull to storedefault values in a configuration file like ``sample-program.cfg``

1. Imports :~~~~~~~~~~~~

First of all, you just need the following classes to build your own script :

* **TestCommand :** This command class will print to stdout the inputs args and the configuration file content.

* **BasicProgram :** The most simple program to run your command classes.

* **SimpleSection :** This class is used to declare a Section in the config file (ConfigFile)

* **Element :** This class is used to declare an Option (a field) in the previous section.

* **Base64ElementHook :** This hook is used as a post reading processing in order to convert base64 data stored into the config file into plain text data.

2. Declaration :~~~~~~~~~~~~~~~~

There is a script called sample-program2.py which contains all the followinglines of code.

#. Instead of creating a config file, we will use an in-memory config file (**Step 1**)#. You create an other class MyProgram (which extends the BasicProgram) (**Step 2**)#. We override the default method called ``add_config_options``. (**Step 3**)#. We declare the section named ``ldap`` that we are looking for.#. We declare all the fields store into the previous section. For each fied, you can says if it is required, the default value, the type, an optional description. See the documentatino for more details. (**Step 5**).#. The we declare all argparse arguments using the previous configuration declaration. This is very usefull because the data store into the configuration file are used as the default value for the argparse argument. The description, the type, required or not, ... declared in the ``add_config_options`` method are used to configure the parser argument. No need to repeat your self. (**Step 6**)#. Declaration of the ``test`` argument using TestCommand class. (**Step 7**)#. Finally you just have to instanciate your class MyProgram, the first argument is the program name. (**Step 8**)#. We override the default config file name ``'.<program name>.cfg'``. (**Step 9**)#. We launch the program. (**Step 10**)

You can see the variable ``host`` contains the input message ``an.other.host.com`` into theargs object. The option ``port`` contains the default value ``389``.You can also acces to the values store into the configuration file like ``account`` or ``password`` which can not be override by the CLI.