1

I'm looking for a library wrapper class to wrap an unix command into a Python class (, possibly for further presentation of command on the web). Ideally the command options are presented in the docstring of my wrapped class, and the library either automatically (or by method calls) extracts the optional and required arguments. Have you seen or heard of such a library?

Details related to function of library wrapper class

Lets say I want to wrap a set of commands in a dedicated Python class, possibly for further presentation on a web page which again could present the different options available for the command. Does such a library already exist?

Here are some further explanation on what I would like out of the library, if it exists, based on a somewhat simplistic command, helloworld, with the following usage:

helloworld [-t <text>] [-n <num>] -U  
    -U          uppercase the output text  
    -t <text>   text to display, defaults to [Hello world]  
    -n <num>    Number of repeats  

To wrap this command I would like to write a Python class somewhat similar to the following:

class HelloWorld(CommandWrapper):
    """Wrapper around helloworld command.

    Options:
       use_uppercase   -U         Uppercase the output text [false]
       text            -t <text>  Text to display [Hello World]
       repeats         -n <num>   Number of repeats [1]
    """

    def __init__(self, *args):

       self.build_options(__doc__, 'helloworld', args)

Now this could be used as follows:

 all_defaults = HelloWorld()
 with_args = HelloWorld(number=4, "Cool")

 print("Command with all defaults: {}".format(all_defaults))
 print("Command with arguments: {}".format(with_args))

which would print the following

Command with all defaults: helloworld
Command with arguments: helloworld -n 4 -t Cool

Optionally, this library wrapper class would also provide some elements/lists/dicts to be used on a web page where it was possible to get the name of the command, and what the various options are available (possibly with default options). Based on this information one could build a form where it was possible to add options, and choose suitable values for the added options.

3
  • It sounds like you are looking for something similar to ExplainShell: github.com/idank/explainshell Commented Feb 13, 2017 at 20:29
  • The question isn't clear enough for me. Is the docstring extraction the main requirement, or is it a (very, very, very, very, very) nice-to-have? Is the wrapper supposed to actually invoke the command it wraps, or just generate a string that represents a command which could be executed (if typed at a shell prompt)? Given that I'm still confused by this question, my best guess for something that is worth looking into is docopt.
    – John Y
    Commented Jan 12, 2018 at 0:03
  • @JohnY, the class should execute the command, with an interface to the various arguments. The trick is that the library should build the wrapper class...
    – holroy
    Commented Jan 30, 2018 at 13:49

2 Answers 2

0

Perhaps try the 'sh' module

pip install sh

It lets you call commands as if they where python functions

from sh import ifconfig print ifconfig("eth0")

https://amoffat.github.io/sh/

1
  • 1
    This library allows for calling a wrapped command, but it does no checking or document which arguments are legal or not. So it allows for random argument handling, and that is just what I would like to avoid!
    – holroy
    Commented Feb 19, 2016 at 2:49
0

On careful reading of the question I gather that you are looking to present the user with the options that are valid or might be added to a specific command more than anything else so I would suggest as a starting point explainshell.

The aim of explain shell is to parse man pages so as to determine what the possible arguments are and explain a given command-line by matching each argument to the relevant help text in the man page.

The current version of explainshell includes a matcher to parse a given command line and present the user with the relevant help text whereas you would need a constructor to add options, with valid arguments, from a user interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.