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

S

Santosh Kumar

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?
 
S

Steven D'Aprano

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.
 
S

Steven D'Aprano

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top