optparse options

Discussion in 'Python' started by icarus, May 19, 2009.

  1. icarus

    icarus Guest

    Why do I need to put two options for this script to print the path?

    if I just specify the option and argument...
    $ python <script>.py -p xxxx
    Usage: <script>.py [-p dir] [--part=dir]

    <script>.py: error: No options specified

    So I need to give it two arguments...
    $ python <script>.py --part xxxx y
    xxxx


    Desired output:
    $ python <script>.py --part xxxx
    xxxx

    $ python <script>.py --part xxxx y
    Usage: <script>.py [-p dir] [--part=dir]
    <script>.py: error: some message


    ----
    #/usr/bin/python

    import optparse

    def main():
    parser = optparse.OptionParser(usage="%prog [-p dir] [--part=dir] ",
    version="%prog 1.0")

    parser.add_option( "-p", "--part", dest="directory",
    help="process target directory", metavar="dir")
    (options, args) = parser.parse_args()

    if len(args) != 1:
    parser.error("No options specified")


    path = options.directory
    print path

    if __name__ == "__main__":
    main()
    icarus, May 19, 2009
    #1
    1. Advertising

  2. icarus

    Robert Kern Guest

    On 2009-05-19 16:07, icarus wrote:
    > Why do I need to put two options for this script to print the path?
    >
    > if I just specify the option and argument...
    > $ python<script>.py -p xxxx
    > Usage:<script>.py [-p dir] [--part=dir]
    >
    > <script>.py: error: No options specified
    >
    > So I need to give it two arguments...
    > $ python<script>.py --part xxxx y
    > xxxx


    You have this piece of code:

    if len(args) != 1:
    parser.error("No options specified")

    The args list contains all of the arguments after the options. Unlike sys.argv,
    it does not contain the executable's name in the first slot.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, May 19, 2009
    #2
    1. Advertising

  3. Ben Finney wrote:
    > icarus <> writes:
    >
    >> parser = optparse.OptionParser(usage="%prog [-p dir] [--part=dir] ",
    >> version="%prog 1.0")
    >>
    >> parser.add_option( "-p", "--part", dest="directory",
    >> help="process target directory", metavar="dir")
    >> (options, args) = parser.parse_args()


    ...

    >> if len(args) != 1:
    >> parser.error("No options specified")

    >
    > The message is confusing, since it doesn't match the condition; it would
    > be correct to say “Did not specify exactly one non-option argumentâ€.
    >
    > In this case, it looks like you don't want to check this at all, and
    > should instead operate on the basis of the options only.


    I also wanted to note that it looks quite illogical and
    counter-intuitive to create "required options", since by definition they
    should be optional.
    Try using arguments instead, with some type-switching flags, if
    necessary - it should make CLI more consistent and save some typing by
    omitting otherwise always-required option argument ("--part").

    --
    Mike Kazantsev // fraggod.net


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.11 (GNU/Linux)

    iEYEARECAAYFAkoTt1IACgkQASbOZpzyXnEYRwCgkMwKuOnkbrdIr3MSVo24YEcp
    eyoAn27se+NLYO8OC8nYLDwg/o6SWS3V
    =vWBf
    -----END PGP SIGNATURE-----
    Mike Kazantsev, May 20, 2009
    #3
    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. David Goodger

    Re: Iterating over optparse options

    David Goodger, Jun 25, 2003, in forum: Python
    Replies:
    0
    Views:
    977
    David Goodger
    Jun 25, 2003
  2. Gregory (Grisha) Trubetskoy

    Required options in optparse

    Gregory (Grisha) Trubetskoy, Oct 4, 2003, in forum: Python
    Replies:
    0
    Views:
    409
    Gregory (Grisha) Trubetskoy
    Oct 4, 2003
  3. Steven Bethard
    Replies:
    0
    Views:
    384
    Steven Bethard
    May 10, 2006
  4. Rocky Zhou
    Replies:
    3
    Views:
    298
    Steven Bethard
    Mar 18, 2007
  5. Omari Norman

    Re: optparse - required options

    Omari Norman, Aug 23, 2007, in forum: Python
    Replies:
    2
    Views:
    417
    Steve Holden
    Aug 24, 2007
Loading...

Share This Page