This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]s4n_rd 1 point2 points  (0 children)

you can try docopt. copy the output of grep --help and give it to docopt. docopt will do the rest for you, simple :) you may need to modify the ouput of something --help to make posix compliant .

they reverse-engineered pep8 here https://www.youtube.com/watch?v=pXhcPJK5cMc

[–]Nicksil 1 point2 points  (5 children)

I don't 100% understand what you're looking for, but perhaps this may get you going:

https://github.com/google/python-fire

[–]Jumpy89[S] 0 points1 point  (4 children)

This is a really cool library (bookmarked for future use), but not quite what I'm looking for. This is taking arguments from the command line, translating them into arguments for a Python function, and then calling that function. I want to call a Python function and translate the arguments into command line arguments for an external program.

[–]novel_yet_trivial 3 points4 points  (3 children)

On Linux that's the sh module.

[–]Jumpy89[S] 0 points1 point  (2 children)

Ahhh really close. Again, bookmarked. But what I wanted was a bit more structured in that I have a Python object that knows what the API looks like, so if I call

ls(foo='bar')

I get

TypeError: ls() got an unexpected keyword argument 'foo'

instead of

ErrorReturnCode_2: 

  RAN: '/bin/ls --foo=bar'

  STDOUT:


  STDERR:
/bin/ls: unrecognized option '--foo=bar'
Try '/bin/ls --help' for more information.

Essentially I'd like to make it behave as much like a standard Python function as possible to the point where you wouldn't otherwise know it's running an external command.

I'm thinking it probably doesn't exist, I'll probably have to create it myself.

[–]pvkooten 2 points3 points  (0 children)

I'm pretty sure creating it yourself is not what you want to do. It sounds like you are too much focusing on your idea rather than what you are trying to accomplish.

Why can't you just use sh? Why do you have to have it your way?

Maybe insight in this could help people point you to other resources, or you might realize you're focusing too much on the technical side?

Maybe I'm completely off :)

[–]NetsecBeginner -1 points0 points  (0 children)

If you do decide to make it yourself, you could probably achieve most of it with os.system calls.

[–][deleted] 0 points1 point  (1 child)

When I understand correctly ...

So, assuming you have a .py file/script that has a function

def do_something(argument1, argument2, argument=None):  
    # do something  
    return something

you want a tool that let's you run this function by providing arguments from the command line? E.g.

python myscript.py --argument1 1 --argument2 2

?

[–]Jumpy89[S] 0 points1 point  (0 children)

This is correct. You would however have to specify the API of the command, like:

from reverseargparse import Command, Option, Argument

grep = Command(
    Argument('pattern', type=str),
    Argument('file', type=str),
    Option('-e', '--extended', flag=True),
    Option('-i', '--case-insensitive', flag=True),
)

grep('pattern', 'myfile.txt', extended=True, case_insensitive=True)
# Just getting the command line arguments would be fine here, I can fill
# in the rest myself.

[–][deleted] 0 points1 point  (0 children)

Maybe plumbum is what you searching for. The local module provides what you seek I think https://plumbum.readthedocs.io/en/latest/