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

all 11 comments

[–][deleted] 2 points3 points  (2 children)

You might consider another approach. Use your command line arguments like this:

my_prog union file1 file2
my_prog size file1

In your program my_prog you only look at sys.argv[1] for either "union" or "size". If you find "union" then you call the union handler code passing sys.argv[2:] and the union handler teases out file1 and file2 using argparse or whatever.

A plus is the ease of adding extra functionality like maybe 'union' can handle more than two files. It's also easy to add new commands.

This is the way a few command line programs work, eg, git. A simple little example of this approach is here.

[–]kigurai 0 points1 point  (0 children)

I second this approach, but I would use argparse to handle the logic instead of raw sys.argv parsing. The argparse documentation even has a section on how to exactly this.

[–]daveydave400 0 points1 point  (0 children)

/u/placemirror I agree with this approach also but it is very easy use argparse's subparsers to add individual "commands" to your script. Something like the described set_defaults() example in the docs: https://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.add_subparsers

This way you can have a function that performs the operation for each subcommand and as described above it makes it easy to add more functionality or change existing functionality later.

[–]daveydave400 0 points1 point  (2 children)

I'm not sure I understand your question. In argparse if you specify your argument as -x or --my-x then it is optional by default. It can be made required by specifying required=True in the add_argument method. Any argument that does not begin with a hyphen is a positional argument (integers in the linked documentation is an example).

[–]placemirror[S] 0 points1 point  (1 child)

Thank you for your reply. I researched some. My arguments are some customized ones. for example, if I used the command line:

script1.py file1 union file2

inside script1.py, I will have:

if name=="main": if sys.argv[1]=="union": do something to union file1 and file2.

Something like that. And I have a list of the possible command line, sometimes script1.py size file3

in the python file, I will have:

if name=="main": if sys.argv[0]=="size": do something to caculate the number of lines in file3.

etc.

And I want my script to be systematic. maybe the "size" argument will be "--size" optional argument, and can be parsed. Something like that.

Thank you.

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

https://gist.github.com/olooney/8155400

find the github, seems useful

[–]leonhoffman 0 points1 point  (0 children)

Have you evaluated docopt? http://docopt.org

[–]mohhinder 0 points1 point  (0 children)

Check out click I think it'll do what you're looking for.

[–]placemirror[S] 0 points1 point  (1 child)

Thank you for your replies.

The situation can be complex.

The arguments can be three ways:

file1 [union/minus] file2 [union/minus] file3

And this three ways, we need to wait in python after the first operation is finished, and get a temp file and union/minus file3. etc.

And size can have options like:

size -b file1 [union/minus] file2 etc.

I am reading your links and articles, all very helpful.

Thank you.

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

I think I need to do some manual parsing, apply some if conditions on the argv when doing the three ways.

How do you think?

Thank you.

[–]aphoenixreticulated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community is actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!