473,503 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optional arguments with compact reporting in optparse

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

Nov 8 '07 #1
2 2896
braver wrote:
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
Nov 8 '07 #2
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

Nov 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1384
by: David Stockwell | last post by:
Whats a good way to get arguments off of the command line? Thanks David ------- Tracfone: http://cellphone.duneram.com/index.html Cam: http://www.duneram.com/cam/index.html Tax:...
3
2452
by: Tomi Silander | last post by:
Hi, this must have been asked 1000 times (or nobody is as stupid as me), but since I could not find the answer, here is the question. My program mitvit.py: -------------- import optparse...
1
1364
by: Alex Gittens | last post by:
I would like my program to accept a list of range values on the command line, like -a 1 -a 1-10 -a 4,5,2 In the interest of avoiding reinventing the wheel, is there already available code for...
3
3539
by: olaufr | last post by:
Hi, I need to call a python script, with command line arguments (it is an autonomous script with a __main__), from within another python script. Can I use exec() or execfile() for this? How to...
0
1130
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks...
14
3245
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
8
9924
by: Ritesh Raj Sarraf | last post by:
Hi, I'm having some minor problems with optparse. I'm just worried that someone shouldn't say that multiple argument feature isn't implemented in optpartse. How to tackle multiple arguments...
6
2691
by: Rob Hoelz | last post by:
So these two functions are different: void foo(void); and void foo(); because the first one allows no arguments, but the second does. My question is: In the implementation of the...
3
1937
by: Rocky Zhou | last post by:
I wonder is there any way to make the wrapper program can wrap options && arguments for the the subprocess/command the wrapper will execute? by getopt or optparse module? This is something...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.