Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > want to show list of available options and arguments in my commandline utility

Reply
Thread Tools

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

 
 
Santosh Kumar
Guest
Posts: n/a
 
      09-16-2012
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?
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-16-2012
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-16-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
loselong (new commandline utility) Malcolm McLean C Programming 2 03-25-2013 03:01 PM
Re: want to show list of available options and arguments in mycommand line utility Peter Otten Python 0 09-16-2012 07:23 AM
commandline options and output-"method" Skave Rat Ruby 3 09-10-2008 10:00 PM
my module and unittest contend over commandline options... chrisber Python 1 05-02-2008 08:06 PM
Option parser question - reading options from file as well as commandline Andrew Robert Python 8 05-22-2006 07:18 PM



Advertisments