optional arguments with compact reporting in optparse

B

braver

Posted to the Optik list, but it seems defunct. Optik is now Python's
optparse.

I wonder how do you implement optional arguments to Optik. I.e., you
can have an option

-P [file]

-- the filename is optional, with a default "data,pikl". It works as
follows:

-- if no -P is given, no pickle is written
-- if -P is given without the filename following, a pickle file is
written with the default name data.pikl
-- if -P filename is given, the pickle is written to filename

How do we do optional values in Optik, with nargs <= 1?

Another question I have it how can I output a docstring right from the
parser.add_option() block? I prefer to define all option-related
things in the same block once. I'd like to output the help value from
the block, or append it to a report.

Here's example from my Ruby wrapper for Ruby's optparse:

name = :divisor
help = "show divisor"
short = "-m"
opt.on(short, "--#{name} [STR]", help) do |val|
hash[:show_divisor] = true
hash[:divisor] = val if val
end
report << [name,short,help]

-- notice that my report list is built with the exact values of all
options, regardless of whether they're encountered or not. The I
simply walk through the report list to print a report for this run:

report.each do |name,short,help|
val = opts.name || "----"
STDERR.printf "--%s (%s)\t\%s\t%s\n", o, short, val, help
end if verbose

How can I group such reporting together with add_option in Optik?

Cheers,
Alexy
 
S

Steven Bethard

braver said:
Posted to the Optik list, but it seems defunct. Optik is now Python's
optparse.

I wonder how do you implement optional arguments to Optik.

You may want to check out argparse:

http://argparse.python-hosting.com/

It supports optional arguments like this::

parser = argparse.ArgumentParser()
parser.add_argument('-P', metavar='file', nargs='?', const='data')
args = parser.parse_args()
if args.file is not None:
# -P was supplied, so do something with the file
# if no argument to -P was given, it will be 'data'

Another question I have it how can I output a docstring right from the
parser.add_option() block? I prefer to define all option-related
things in the same block once. I'd like to output the help value from
the block, or append it to a report.

Here's example from my Ruby wrapper for Ruby's optparse:

I don't understand what you want here (and I don't understand the Ruby
code). Can you try explaining what you're trying to do here a different
way?

STeVe
 
B

braver

Steve -- thanks for your pointer to argparse, awesome progress --
optional arguments.

However, I still wonder how I do reporting. The idea is that there
should be a list with tuples of the form:

(short, long, value, help)

-- for all options, regardless of whether they were specified on the
command line or not.

Moreover, a way to create such list should be incremental -- in ruby I
define things around each option definition and add a new tuple to the
reporting list right after the block defining the option.

The idea is, -- help should report on all options with their actual or
default values, and everything pertaining to one option -- its
definition, callbacks, and adding it to the reporting list -- must be
in vicinity of each other, so I don't forget to output some stuff
about an option ten options below... The snippet I've shown pretty
much does it in Ruby.

Ah, yeah, and another thing -- I want to define a module with about 10
options common for a project. Then I'd import it in scripts, and
there should be three things doable with the preexisting default
options:

-- turn off any of them
-- supply new defaults to any of them
-- add new options in the same way

Wonder how brave pythonistas do that!
Cheers,
Alexy
 
S

Steven Bethard

braver said:
Steve -- thanks for your pointer to argparse, awesome progress --
optional arguments.

However, I still wonder how I do reporting. The idea is that there
should be a list with tuples of the form:

(short, long, value, help)

-- for all options, regardless of whether they were specified on the
command line or not.

Moreover, a way to create such list should be incremental -- in ruby I
define things around each option definition and add a new tuple to the
reporting list right after the block defining the option.

The idea is, -- help should report on all options with their actual or
default values, and everything pertaining to one option -- its
definition, callbacks, and adding it to the reporting list -- must be
in vicinity of each other, so I don't forget to output some stuff
about an option ten options below... The snippet I've shown pretty
much does it in Ruby.

I guess I still don't understand how that's different from what
add_argument (or add_option) and print_help currently do::
usage: [-h] [-P [P]] [--foo FOO]

optional arguments:
-h, --help show this help message and exit
-P [P] PP
--foo FOO FOO

Everything pertaining to one option is grouped together in a single
add_argument call (add_option if you're using optparse). And you don't
forget what to output because the print_help() method already takes care
of it all. Or are you asking for more information (e.g. default values)
to be included in the help output? I've been hesitant to do that
automatically because often the default values do not have a useful
printable form (e.g. when the default is a function or class object).
Ah, yeah, and another thing -- I want to define a module with about 10
options common for a project. Then I'd import it in scripts, and
there should be three things doable with the preexisting default
options:

-- turn off any of them
-- supply new defaults to any of them
-- add new options in the same way

Wonder how brave pythonistas do that!

With argparse, it would look something like::

import mod
parser = argparse.ArgumentParser(parents=[mod.parser])
# supply new default
parser.set_defaults(foo='bar')
# add new option
parser.add_argument('--bar', ...)

The parents= keyword argument is documented here:

http://argparse.python-hosting.com/wiki/ArgumentParser/parents

Currently, there isn't a way to turn off an argument. I'd instead
suggest that you create a new parser that doesn't have the extra
options, and have the parser with more options include that one using
parents=.

STeVe
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top