1

let me explain what I have in mind to do in order to give you some context. I have a bunch of python scripts ( that use argpars or optpars ) and their outputs can be usually on the consolle in json, plaint text or csv format.

I would like to build an webapp ( angular + node for instance) that generates automatically a web page for each of my script including some input box for any of the argument needed by the python script in order to run them form the UI.

I do not want to write, for each python script, the list and type of arguments that they needs to be run, but I am looking for an automatic way to extract such list form each python script itself.

I can try to parse the -h output for each of the script or parse the script itself ( add_option) but it maybe error prone.

Are you aware of any tools/script/module that allow me to do it automatically?

Thanks a lot.

5
  • Assuming that each script supports -h, maybe docopt could parse the result. Commented May 2, 2016 at 17:11
  • @Robᵩ Thanks, but it looks more an alternative to argparse ect.. Commented May 2, 2016 at 17:17
  • You misunderstand. I mean that you should run somescript -h, capture the output, pass that output to docopt's parser, and use the resulting data structure to generate your HTML. You would not be using doctopt as it was intended. Commented May 2, 2016 at 17:23
  • Docopt makes the options definition be the -h output, so it will help where argparse would be prone to inconsistencies. Commented May 2, 2016 at 17:24
  • @Robᵩ Cool idea but, I just made a test, and it is working so and so for me, for instance """test.py usage: spyder.py [-h] [-i INPUT] [-o OUTPUT] optional arguments: -h, --help show this help message and exit -i INPUT, --input INPUT Input log file -o OUTPUT, --output OUTPUT Output """ Is parsed as: {'--help': False, '--input': None, '--output': None} would be cool to get the help itself too Commented May 2, 2016 at 17:31

2 Answers 2

1

The inspect module will help here:

>>> import inspect
>>> def example_function(a, b, c=1234):
        pass
>>> inspect.getargspec(example_function)
ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=(1234,))
Sign up to request clarification or add additional context in comments.

1 Comment

The plac package (pypi.python.org/pypi/plac) uses inspect like this to create a parser that can run such a function. It creates arguments that correspond to the functions arguments. But I don't think that helps with inspecting a script that is already using argparse.
1

You may have to elaborate on what access you have to the scripts and their parsers. Are they black boxes that you can only invoke with -h and get back a help or usage message, or can you inspect the parser?

For example when using argparse, you make a parser, assign it some attributes and create argument Actions. Those action objects are collected in a list, parser._actions.

Look at parser.format_help and parser.format_usage methods to see what values are passed to the help formatter to create the string displays.

Apart from examining the argparse.py file, I'd suggest creating a parser in an interactive session, and examine the objects that are created.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.