want to show list of available options and arguments in my commandline utility

Discussion in 'Python' started by Santosh Kumar, Sep 16, 2012.

  1. I have a script that takes an which basically takes a command line
    argument and prints after processing. If I don't give the argument to
    the script, it gives me a ValueError:

    ValueError: need more than 1 value to unpack

    I was trying to utilizing this space, if there is no argument I want it
    to show:
    usages: myscriptpy [option] [argument]

    options:
    --help print this help message and exit

    Nothing more than that. I was looking on the argparse module, it can
    do the stuffs I want, but
    I don't to rewrite and mess up my current script. What should I do?
    Santosh Kumar, Sep 16, 2012
    #1
    1. Advertising

  2. Re: want to show list of available options and arguments in mycommandline utility

    On Sun, 16 Sep 2012 12:20:08 +0530, Santosh Kumar wrote:

    > I have a script that takes an which basically takes a command line
    > argument and prints after processing. If I don't give the argument to
    > the script, it gives me a ValueError:
    >
    > ValueError: need more than 1 value to unpack
    >
    > I was trying to utilizing this space, if there is no argument I want it
    > to show:
    > usages: myscriptpy [option] [argument]
    >
    > options:
    > --help print this help message and exit
    >
    > Nothing more than that. I was looking on the argparse module, it can do
    > the stuffs I want, but
    > I don't to rewrite and mess up my current script. What should I do?


    Then don't mess it up.


    What you are basically asking for sounds like "I want to add this cool
    feature to my project, but I don't want to actually do the work of adding
    the feature. How can I snap my fingers and make it work just like magic?"

    The answer is, you can't.

    Either:

    1) Do without the feature; or

    2) Program the feature.

    For number 2), you can either modify your script to use argparse to deal
    with the argument handling (which is probably simplest), or you can try
    to duplicate argparse's functionality yourself.

    Make a backup copy first -- never, ever, ever work on the only copy of a
    working script. Or better still, use a proper VCS (version control
    system) to manage the software. But for small changes and simple needs, a
    good enough solution is "always edit a copy".

    In this case, it *may* be simple enough to duplicate the functionality.
    There's no way to tell, because you don't show any code, but my *guess*
    is that you have a line like:

    a, b = sys.argv

    and if you don't provide any command-line arguments, that fails with

    ValueError: need more than 1 value to unpack

    (Lots and lots and lots of other things will also fail with the same
    error.)

    Is this case, the fix is simple:

    args = sys.argv[1:] # ignore the first entry, which is unimportant
    if len(args) == 1:
    arg = args[0]
    process(arg, default_option)
    elif len(args) == 2:
    option, arg == args
    process(arg, option)
    elif len(args) > 2:
    do_error('too many arguments')
    else:
    do_error('not enough arguments')

    But as the argument checking code gets more extensive, it is time to just
    use argparse or one of the other libraries for argument checking instead.



    --
    Steven
    Steven D'Aprano, Sep 16, 2012
    #2
    1. Advertising

  3. Re: want to show list of available options and arguments in mycommandline utility

    On Sun, 16 Sep 2012 12:20:08 +0530, Santosh Kumar wrote:

    > I have a script that takes an which basically takes a command line
    > argument and prints after processing. If I don't give the argument to
    > the script, it gives me a ValueError:
    >
    > ValueError: need more than 1 value to unpack
    >
    > I was trying to utilizing this space, if there is no argument I want it
    > to show:
    > usages: myscriptpy [option] [argument]
    >
    > options:
    > --help print this help message and exit
    >
    > Nothing more than that. I was looking on the argparse module, it can do
    > the stuffs I want, but
    > I don't to rewrite and mess up my current script. What should I do?


    Then don't mess it up.


    What you are basically asking for sounds like "I want to add this cool
    feature to my project, but I don't want to actually do the work of adding
    the feature. How can I snap my fingers and make it work just like magic?"

    The answer is, you can't.

    Either:

    1) Do without the feature; or

    2) Program the feature.

    For number 2), you can either modify your script to use argparse to deal
    with the argument handling (which is probably simplest), or you can try
    to duplicate argparse's functionality yourself.

    Make a backup copy first -- never, ever, ever work on the only copy of a
    working script. Or better still, use a proper VCS (version control
    system) to manage the software. But for small changes and simple needs, a
    good enough solution is "always edit a copy".

    In this case, it *may* be simple enough to duplicate the functionality.
    There's no way to tell, because you don't show any code, but my *guess*
    is that you have a line like:

    a, b = sys.argv

    and if you don't provide any command-line arguments, that fails with

    ValueError: need more than 1 value to unpack

    (Lots and lots and lots of other things will also fail with the same
    error.)

    Is this case, the fix is simple:

    args = sys.argv[1:] # ignore the first entry, which is unimportant
    if len(args) == 1:
    arg = args[0]
    process(arg, default_option)
    elif len(args) == 2:
    option, arg == args
    process(arg, option)
    elif len(args) > 2:
    do_error('too many arguments')
    else:
    do_error('not enough arguments')

    But as the argument checking code gets more extensive, it is time to just
    use argparse or one of the other libraries for argument checking instead.



    --
    Steven
    Steven D'Aprano, Sep 16, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Robert
    Replies:
    8
    Views:
    348
    Tim N. van der Leeuw
    May 22, 2006
  2. chrisber
    Replies:
    1
    Views:
    372
    chrisber
    May 2, 2008
  3. Skave Rat
    Replies:
    3
    Views:
    84
    Skave Rat
    Sep 10, 2008
  4. Peter Otten
    Replies:
    0
    Views:
    165
    Peter Otten
    Sep 16, 2012
  5. Malcolm McLean

    loselong (new commandline utility)

    Malcolm McLean, Mar 13, 2013, in forum: C Programming
    Replies:
    2
    Views:
    174
    Malcolm McLean
    Mar 25, 2013
Loading...

Share This Page