optparse -- anyway to find if the user entered an option?

Discussion in 'Python' started by Karthik Gurusamy, Apr 15, 2007.

  1. Hi,

    I see that I can provide a default value for an option. But I couldn't
    find out any way if the user really entered the option or the option
    took that value because of default. A simple check for value with
    default may not always work as the user might have manually
    entered the same default value.

    Let's assume I want to take in the ip-address using -i <ip-addr>.
    If user didn't give it explicitly, I am going to use socket interface
    to figure out this host's IP address.

    ip_addr_default = '100.100.100.100'

    parser.add_option("-i", "--ip-address", dest="ip",
    default=ip_addr_default,
    metavar="IP-ADDRESS", help="IP address. default:" +
    ip_addr_default + "e.g. --i=1.1.1.1"
    )

    (options, args) = parser.parse_args()

    Now if options.ip == ip_addr_default, I still can't be 100% sure that
    the user did not type -i 100.100.100.100.
    Any way to figure out from options that the user typed it or not?

    (The reason I want to know this is if user did not mention -i, I can
    compute IP later
    using socket module)

    I could think of a hack of using None as default and since no user can
    ever
    enter a None value, I can be sure that the user didn't provide -i.
    I'm wondering if there is a cleaner approach -- something like
    parser.opt_seen("-i")

    Thanks,
    Karthik
     
    Karthik Gurusamy, Apr 15, 2007
    #1
    1. Advertising

  2. Karthik Gurusamy

    James Stroud Guest

    Karthik Gurusamy wrote:
    > Hi,
    >
    > I see that I can provide a default value for an option. But I couldn't
    > find out any way if the user really entered the option or the option
    > took that value because of default. A simple check for value with
    > default may not always work as the user might have manually
    > entered the same default value.
    >
    > Let's assume I want to take in the ip-address using -i <ip-addr>.
    > If user didn't give it explicitly, I am going to use socket interface
    > to figure out this host's IP address.
    >
    > ip_addr_default = '100.100.100.100'
    >
    > parser.add_option("-i", "--ip-address", dest="ip",
    > default=ip_addr_default,
    > metavar="IP-ADDRESS", help="IP address. default:" +
    > ip_addr_default + "e.g. --i=1.1.1.1"
    > )
    >
    > (options, args) = parser.parse_args()
    >
    > Now if options.ip == ip_addr_default, I still can't be 100% sure that
    > the user did not type -i 100.100.100.100.
    > Any way to figure out from options that the user typed it or not?
    >
    > (The reason I want to know this is if user did not mention -i, I can
    > compute IP later
    > using socket module)
    >
    > I could think of a hack of using None as default and since no user can
    > ever
    > enter a None value, I can be sure that the user didn't provide -i.
    > I'm wondering if there is a cleaner approach -- something like
    > parser.opt_seen("-i")
    >
    > Thanks,
    > Karthik
    >


    Using None wouldn't be a hack, it would rather be a common and
    straightforward python idiom.

    Compare:

    if parser.opt_seen("-i"):
    do_whatever()

    to

    if options.ip is None:
    do_whatever()

    Looks like the second even saves a little typing. After using the former
    a while, I would venture to guess that you might realize how the latter
    is actually cleaner.

    James
     
    James Stroud, Apr 15, 2007
    #2
    1. Advertising

  3. On Sat, 14 Apr 2007 16:49:22 -0700, Karthik Gurusamy wrote:

    > I'm wondering if there is a cleaner approach -- something like
    > parser.opt_seen("-i")


    What do dir(parser) and help(parser) say?



    --
    Steven.
     
    Steven D'Aprano, Apr 15, 2007
    #3
  4. On Apr 14, 7:54 pm, Steven D'Aprano
    <> wrote:
    > On Sat, 14 Apr 2007 16:49:22 -0700, Karthik Gurusamy wrote:
    > > I'm wondering if there is a cleaner approach -- something like
    > > parser.opt_seen("-i")

    >
    > What do dir(parser) and help(parser) say?


    They don't seem to convey existence of a routine like the
    one I'm looking for. I did check the lib reference - I guess
    such a support is not available. Most likely the 'None'
    solution will work for me. I will go ahead with it.

    Karthik


    >>> dir(parser)

    ['__doc__', '__init__', '__module__', '_add_help_option',
    '_add_version_option', '_check_conflict', '_create_option_list',
    '_create_option_mappings', '_get_all_options', '_get_args',
    '_get_encoding', '_init_parsing_state', '_long_opt',
    '_match_long_opt', '_populate_option_list', '_process_args',
    '_process_long_opt', '_process_short_opts', '_share_option_mappings',
    '_short_opt', 'add_option', 'add_option_group', 'add_options',
    'allow_interspersed_args', 'check_values', 'conflict_handler',
    'defaults', 'description', 'destroy', 'disable_interspersed_args',
    'enable_interspersed_args', 'epilog', 'error', 'exit',
    'expand_prog_name', 'format_description', 'format_epilog',
    'format_help', 'format_option_help', 'formatter',
    'get_default_values', 'get_description', 'get_option',
    'get_option_group', 'get_prog_name', 'get_usage', 'get_version',
    'has_option', 'largs', 'option_class', 'option_groups', 'option_list',
    'parse_args', 'print_help', 'print_usage', 'print_version',
    'process_default_values', 'prog', 'rargs', 'remove_option',
    'set_conflict_handler', 'set_default', 'set_defaults',
    'set_description', 'set_process_default_values', 'set_usage',
    'standard_option_list', 'usage', 'values', 'version']
    >>>
    >>> print sys.version

    2.5 (r25:51908, Sep 29 2006, 12:35:59)
    [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-54)]

    help(parser) just gives info on a generic instance.

    >
    > --
    > Steven.
     
    Karthik Gurusamy, Apr 15, 2007
    #4
  5. James Stroud wrote:
    > Karthik Gurusamy wrote:
    >> Hi,
    >>
    >> I see that I can provide a default value for an option. But I couldn't
    >> find out any way if the user really entered the option or the option
    >> took that value because of default. A simple check for value with
    >> default may not always work as the user might have manually
    >> entered the same default value.
    >>
    >> Let's assume I want to take in the ip-address using -i <ip-addr>.
    >> If user didn't give it explicitly, I am going to use socket interface
    >> to figure out this host's IP address.
    >>
    >> ip_addr_default = '100.100.100.100'
    >>
    >> parser.add_option("-i", "--ip-address", dest="ip",
    >> default=ip_addr_default,
    >> metavar="IP-ADDRESS", help="IP address. default:" +
    >> ip_addr_default + "e.g. --i=1.1.1.1"
    >> )
    >>
    >> (options, args) = parser.parse_args()
    >>
    >> Now if options.ip == ip_addr_default, I still can't be 100% sure that
    >> the user did not type -i 100.100.100.100.
    >> Any way to figure out from options that the user typed it or not?
    >>
    >> (The reason I want to know this is if user did not mention -i, I can
    >> compute IP later
    >> using socket module)
    >>
    >> I could think of a hack of using None as default and since no user can
    >> ever
    >> enter a None value, I can be sure that the user didn't provide -i.
    >> I'm wondering if there is a cleaner approach -- something like
    >> parser.opt_seen("-i")
    >>
    >> Thanks,
    >> Karthik
    >>

    >
    > Using None wouldn't be a hack, it would rather be a common and
    > straightforward python idiom.


    I agree. Also, remember that in optparse the default default (if you
    will) is None.
    --
    Michael Hoffman
     
    Michael Hoffman, Apr 16, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    1,509
    TN Bella
    Jul 1, 2004
  2. R. Bernstein
    Replies:
    7
    Views:
    494
    Steven Bethard
    Jan 26, 2006
  3. Pupeno
    Replies:
    1
    Views:
    296
    Pupeno
    Jul 27, 2006
  4. Mathias Waack

    optparse option prefix

    Mathias Waack, Apr 4, 2007, in forum: Python
    Replies:
    4
    Views:
    285
    Steven Bethard
    Apr 4, 2007
  5. Nirnimesh

    optparse: list out entered values

    Nirnimesh, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    699
    Steven Bethard
    May 25, 2007
Loading...

Share This Page