question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically create python wrappers for bash script using docopts

See original GitHub issue

Hi,

I have a set of bash script that are now using docopts. I would like to be able to automatically create python function able to call these bash script.

The idea is the following one. I have a script test.sh with

Usage:
  test.sh (-testdir <testDirPath>) [-v=<VERSION>]
  test.sh -h | --help
Options:
  -h --help         Show this screen.
  -testdir <path>   Dew test directory path.
  -v <version>      Version for XX TOOL [default: Prod].

From which I can extract the Usage part with grep/sed. Now I want to use docopt to automatically write a python script containing:

def test(testdir, v='Prod'):
    subprocess.Popen(['test.sh.sh -testdir %s -v %s' %(testDir, v], shell = True).

It seems to be possible to get the arguments from docopt:

arguments = docopt(__doc__, version='Naval Fate 2.0')
    print(arguments)

but how can I override docopt so it does not try parsing the current python script being executed as what I want is only the arguments dictionnary to buid the function test().

from docopt import docopt


with open ("test.doc", "r") as myfile:
    data=myfile.read()
print(data)    
arguments = docopt(data)

Any idea? Is this thing have been done before?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
TylerTempcommented, Aug 26, 2020

If I understand it correctly: you want to access the internal data that been parsed and matched by docopt.

In short: nuh, docopt does not expose such api. May considering copy/paste and modify code from docopt.

My lib allows to access parsed data tree (and the matched status, errors, etc), but, I wont suggested it, as it’s really nested and complex without document/guid of this part

1reaction
laurentvmcommented, Jun 1, 2020

Nope, because in your example, the args are provided and hard coded. What I want is being able to get the dictionnary with typed arguments; default values and so on in order to write (generate) a python wrapper file that defines python function with the correct set of arguments that I can use in python only.

I can write all the wrapping function myself, by hand but it will request eacht time a manual modifiction if my bash function changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

docopts/README.md at master - GitHub
docopts : the command line wrapper for bash. Most concepts are documented in the docopt (without S) manual - see docopt.org. Many examples...
Read more >
Building Beautiful Command Line Interfaces with Python
Docopt. Docopt is a lightweight python package for creating command line interface easily by parsing POSIC-style or Markdown usage instructions.
Read more >
Release 2.10.0 Matˇej Týˇc - Argbash documentation
Argbash (https://argbash.dev) is a bash code generator that can assist you in writing scripts that accept arguments.
Read more >
Wrapping bash scripts in python - Stack Overflow
this script will first create a temp file, then put shell commands in it, and give it execute permissions. and finally run the...
Read more >
Build a Python Directory Tree Generator for the Command Line
tree.py provides an entry-point script for you to run the application. Then you have the rptree/ directory that holds a Python package with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found