3

If possible I would like to use the following structure for a command however I can't seem to figure out how to achieve this in Python:

./somescript.py arg <optional argument> -- "some long argument"

Would it be possible to achieve this in a feasible manner without too much dirty code? Or should I just reconsider the syntax (which is primarily preference).

Thanks!

asked Jun 16, 2010 at 23:04
3
  • Hopefully the shell-scripting tag isn't too specific to literally shell scripting (sh,bash,zsh,etc). Commented Jun 16, 2010 at 23:06
  • I think writing that as ./somescript.py arg --optional_arg="value" is clearer, and more standard. Why do you prefer your approach? Commented Jun 16, 2010 at 23:16
  • Essentially I want to log something so here would be a more real-world example: ./somescript.py add <alias> -- "netstat -nlp | grep pie" Which would optionally assign an alias to that entry - to me its clearer due to the nature of the program (take everything at the end as is). Commented Jun 16, 2010 at 23:22

3 Answers 3

3

I think optparse can do this.

answered Jun 17, 2010 at 1:26
Sign up to request clarification or add additional context in comments.

2 Comments

It does. Everything after "--" is an argument. Everything before is an option.
Mildly embarassed I missed that.. Thanks!
1

If you don't want to use dashes in front of your arg and optional_argument, that's kind of strange by typical Unix command-line behavior, but I don't understand why every answer appears to believe you have to use the dashes. Avoiding them is kind of trivial, actually...:

import sys
def before_and_after_doubledashes(args=sys.argv):
 where_doubledashes = args.index('--') if '--' in args else len(args)
 return args[:where_doubledashes], args[where_doubledashes+1:]

This completely ignores whether args start with dashes or not, just singles out the first occurrence of an arg that's exactly a double dash (if any) and returns a tuple of two lists, one all the args that are before that double dash if any, one all the args that are after it (empty if there's no double dash argument). You can assign these lists from the call:

before, after = before_and_after_doubledashes()

then treat them as you will (check their lengths, assign variables from part of them, etc).

answered Jun 17, 2010 at 2:15

Comments

0

You just need to stick something like this on the first line: #/usr/local/bin/python

Just make yours be wherever your python binary is located.

As for args look at getopt or optparser

And remember to chmod your file to make it executable.

answered Jun 16, 2010 at 23:19

3 Comments

I think you may have misunderstood the question - it's about parsing the arguments in a specific way within the Python program.
hmm yeah I guess I don't quite get the question. How come you can't just use getopt to do this? getopt in python What's different about your syntax?
the user doesn't want to use dashes before his "arg" and "optional arg".

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.