Vincenzo
Vincenzo

Reputation: 85

Extract arguments from python script

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.

Upvotes: 1

Views: 1997

Answers (2)

hpaulj
hpaulj

Reputation: 231385

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.

Upvotes: 1

user6275647
user6275647

Reputation: 381

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,))

Upvotes: 1

Related Questions