How to send negative value arguments for command line application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacob l
    New Member
    • Dec 2010
    • 3

    How to send negative value arguments for command line application?

    I have created a piece of python code; however, I want to execute it from the command line with some arguments and options. What I have so far works ok (with the exception that i CANNOT enter in negative values for the arguments -- and i need to be able to!) How can i better write this code?

    What i enter on the command line:

    C:\...>mycode.p y arg1 arg2 arg3 -o filename -p filename -q filename (this is 3 arguments and 3 options)

    What my code does (works with exception of negatives):

    import sys
    parser=optparse .OptionParser()

    parser.add_opti on("--value1",dest"va l1")
    parser.add_opti on("--value2",dest"va l2")
    parser.add_opti on("--value3",dest"va l3")

    parser.add_opti on("-o",action="stor e",type="string ",dest="opt 1")
    parser.add_opti on("-p",action="stor e",type="string ",dest="opt 2")
    parser.add_opti on("-q",action="stor e",type="string ",dest="opt 3")

    opts,args=parse r.parse_args()


    Help with this please...Again, I want to correctly do the arguments and options on the command line and be able to input negative values for an argument if needed. Again, what i have works (except for negative values)....anyo ne know a better way?
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    AFAIK there is no way to tell the difference between an option and a negative number, so you would have to either assume that non-options are negative numbers, or check with isdigit(), or come up with something like -neg and then the negative number.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      If you come up with a good solution to this please post it as this is something that comes up from time to time.

      Comment

      Working...