Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hex

3 views
Skip to first unread message

hong zhang

unread,
Nov 18, 2009, 9:29:59 PM11/18/09
to pytho...@python.org
List,

I want to input hex number instead of int number. in type="int" in following,

parser.add_option("-F", "--forcemcs", dest="force_mcs", type="int", default=0, help="index of 11n mcs table. Default: 0.")

How can I do it?
Thanks.

--henry



Diez B. Roggisch

unread,
Nov 19, 2009, 3:48:03 AM11/19/09
to
hong zhang schrieb:

You can't. You can get a string, and convert that with e.g.

int("FF", 16)

Or you can extend optparse to know a new type, should be possible. That
would have the advantage that the parser already knows about it & can
reject faulty input.

Diez

Peter Otten

unread,
Nov 19, 2009, 5:01:24 AM11/19/09
to
hong zhang wrote:

Workaround for the lazy: '0xff' on the command line instead of 'ff'.

Rhodri James

unread,
Nov 19, 2009, 6:45:56 PM11/19/09
to pytho...@python.org
On Thu, 19 Nov 2009 02:29:59 -0000, hong zhang <henryz...@yahoo.com>
wrote:

> List,


>
> I want to input hex number instead of int number. in type="int" in
> following,
>
> parser.add_option("-F", "--forcemcs", dest="force_mcs", type="int",
> default=0, help="index of 11n mcs table. Default: 0.")
>
> How can I do it?

Assuming you're talking about "optparse", just prefix the number with "0x"
on the command line:

python myscript.py -F 0xDEAD

If you don't want to have to type the leading "0x", you'll either have to
make yourself a custom type for optparse, or treat it as a string and do
the parsing and error handling afterwards. My recommendation is that you
don't do this: not prefixing your constants if they aren't decimal is an
accident waiting to happen.

--
Rhodri James *-* Wildebeest Herder to the Masses

0 new messages