plac, the easiest command line arguments parser in the world

Discussion in 'Python' started by Michele Simionato, Jun 2, 2010.

  1. I would like to announce to the world the first public release of
    plac:

    http://pypi.python.org/pypi/plac

    Plac is a wrapper over argparse and works in all versions of
    Python starting from Python 2.3 up to Python 3.1.

    With blatant immodesty, plac claims to be the easiest to use command
    line arguments parser module in the Python world. Its goal is to
    reduce the
    learning curve of argparse from hours to minutes. It does so by
    removing the need to build a command line arguments parser by hand:
    actually it is smart enough to infer the parser from function
    annotations.

    Here is a simple example (in Python 3) to wet your appetite:

    $ cat example.py
    def main(arg: "required argument"):
    "do something with arg"
    print('Got %s' % arg)

    if __name__ == '__main__':
    import plac; plac.call(main) # passes sys.argv[1:] to main

    $ python example.py -h
    usage: example.py [-h] arg

    do something with arg

    positional arguments:
    arg required argument

    optional arguments:
    -h, --help show this help message and exit


    $ python example.py
    usage: example.py [-h] arg
    example.py: error: too few arguments

    $ python example.py arg
    Got arg

    $ python example.py arg1 arg2
    usage: example.py [-h] arg
    example.py: error: unrecognized arguments: arg2

    You can find in the documentation a lot of other simple and not so
    simple
    examples:

    http://micheles.googlecode.com/hg/plac/doc/plac.html


    Enjoy!

    Michele Simionato

    P.S. answering an unspoken question: yes, we really needed yet
    another
    command line arguments parser! ;)
     
    Michele Simionato, Jun 2, 2010
    #1
    1. Advertising

  2. Michele Simionato

    Tim Golden Guest

    On 02/06/2010 05:37, Michele Simionato wrote:
    > I would like to announce to the world the first public release of
    > plac:
    >
    > http://pypi.python.org/pypi/plac
    >
    > Plac is a wrapper over argparse and works in all versions of
    > Python starting from Python 2.3 up to Python 3.1.


    I like it. I'm a constant user of the

    def main (a, b=1, c=2):
    # ...

    if __name__ == '__main__':
    main (*sys.argv[1:])

    pattern, which provides a minimally semi-self-documenting
    approach for positional args, but I've always found the existing
    offerings just a little too much work to bother with.
    I'll give plac a run and see how it behaves.

    Thanks

    TJG
     
    Tim Golden, Jun 2, 2010
    #2
    1. Advertising

  3. Michele Simionato

    Paul Rubin Guest

    Tim Golden <> writes:
    > pattern, which provides a minimally semi-self-documenting
    > approach for positional args, but I've always found the existing
    > offerings just a little too much work to bother with.
    > I'll give plac a run and see how it behaves.


    After using optparse a couple of times I got the hang of it. Maybe its
    docs could be organized a bit better, but it does the obvious things
    conveniently once you've figured it out a bit.
     
    Paul Rubin, Jun 2, 2010
    #3
  4. On Jun 2, 10:43 am, Paul Rubin <> wrote:
    > Tim Golden <> writes:
    > > pattern, which provides a minimally semi-self-documenting
    > > approach for positional args, but I've always found the existing
    > > offerings just a little too much work to bother with.
    > > I'll give plac a run and see how it behaves.

    >
    > After using optparse a couple of times I got the hang of it.  Maybe its
    > docs could be organized a bit better, but it does the obvious things
    > conveniently once you've figured it out a bit.


    Notice that optparse is basically useless in the use case Tim is
    considering (positional arguments) since it only manages options.
     
    Michele Simionato, Jun 2, 2010
    #4
  5. Michele Simionato wrote:
    > I would like to announce to the world the first public release of
    > plac:
    >
    > http://pypi.python.org/pypi/plac
    >
    > Plac is a wrapper over argparse and works in all versions of
    > Python starting from Python 2.3 up to Python 3.1.
    >
    > With blatant immodesty, plac claims to be the easiest to use command
    > line arguments parser module in the Python world. Its goal is to
    > reduce the
    > learning curve of argparse from hours to minutes. It does so by
    > removing the need to build a command line arguments parser by hand:
    > actually it is smart enough to infer the parser from function
    > annotations.
    >
    > Here is a simple example (in Python 3) to wet your appetite:
    >
    > $ cat example.py
    > def main(arg: "required argument"):
    > "do something with arg"
    > print('Got %s' % arg)
    >
    > if __name__ == '__main__':
    > import plac; plac.call(main) # passes sys.argv[1:] to main
    >
    > $ python example.py -h
    > usage: example.py [-h] arg
    >
    > do something with arg
    >
    > positional arguments:
    > arg required argument
    >
    > optional arguments:
    > -h, --help show this help message and exit
    >
    >
    > $ python example.py
    > usage: example.py [-h] arg
    > example.py: error: too few arguments
    >
    > $ python example.py arg
    > Got arg
    >
    > $ python example.py arg1 arg2
    > usage: example.py [-h] arg
    > example.py: error: unrecognized arguments: arg2
    >
    > You can find in the documentation a lot of other simple and not so
    > simple
    > examples:
    >
    > http://micheles.googlecode.com/hg/plac/doc/plac.html
    >
    >
    > Enjoy!
    >
    > Michele Simionato
    >
    > P.S. answering an unspoken question: yes, we really needed yet
    > another
    > command line arguments parser! ;)
    >

    Thanks for participating.

    JM
     
    Jean-Michel Pichavant, Jun 2, 2010
    #5
  6. Paul Rubin, 02.06.2010 10:43:
    > Tim Golden writes:
    >> pattern, which provides a minimally semi-self-documenting
    >> approach for positional args, but I've always found the existing
    >> offerings just a little too much work to bother with.
    >> I'll give plac a run and see how it behaves.

    >
    > After using optparse a couple of times I got the hang of it. Maybe its
    > docs could be organized a bit better, but it does the obvious things
    > conveniently once you've figured it out a bit.


    Same from here. I managed to talk a Java-drilled collegue of mine into
    writing a Python script for a little command line utility, but he needed a
    way to organise his argument extraction code when the number of arguments
    started to grow beyond two. I told him that there were two ways to do it:
    do it by hand or do it right. He took the right choice and I took him to
    the optparse docs, copied the first example into his code and we adapted it
    a little. He just loved the beauty of it.

    Stefan
     
    Stefan Behnel, Jun 2, 2010
    #6
  7. On Jun 2, 11:01 am, Stefan Behnel <> wrote:
    > I managed to talk a Java-drilled collegue of mine into
    > writing a Python script for a little command line utility, but he needed a
    > way to organise his argument extraction code when the number of arguments
    > started to grow beyond two. I told him that there were two ways to do it:
    > do it by hand or do it right. He took the right choice and I took him to
    > the optparse docs, copied the first example into his code and we adapted it
    > a little. He just loved the beauty of it.


    Could you show plac to your friend? I would be curious to know what he
    think.
    Perhaps he would call out his judgment on optparse ;)
     
    Michele Simionato, Jun 2, 2010
    #7
  8. optional optional args vs optional positional options

    On Wed, 2 Jun 2010 01:49:18 -0700 (PDT)
    Michele Simionato <> wrote:
    >
    > Notice that optparse is basically useless in the use case Tim is
    > considering (positional arguments) since it only manages options.


    By the way, could you stop naming these "optional arguments", since
    positional arguments can be optional as well? It is confusing :)

    Thanks

    Antoine.
     
    Antoine Pitrou, Jun 2, 2010
    #8
  9. Michele Simionato

    Tim Golden Guest

    Re: optional optional args vs optional positional options

    On 02/06/2010 11:42, Antoine Pitrou wrote:
    > On Wed, 2 Jun 2010 01:49:18 -0700 (PDT)
    > Michele Simionato<> wrote:
    >>
    >> Notice that optparse is basically useless in the use case Tim is
    >> considering (positional arguments) since it only manages options.

    >
    > By the way, could you stop naming these "optional arguments", since
    > positional arguments can be optional as well? It is confusing :)


    The great thing with English is that you can use nouns as
    adjectives without changing them, so you can say "option arguments"
    and "position arguments" quite happily here :)

    But then you run into the fact that you're having semantic arguments
    about argument semantics :(

    TJG
     
    Tim Golden, Jun 2, 2010
    #9
  10. Re: optional optional args vs optional positional options

    +1

    Options are options, arguments are arguments. An optional argument is
    not an option. It is an argument that can be left out.



    On Wed, 2010-06-02 at 12:42 +0200, Antoine Pitrou wrote:
    > On Wed, 2 Jun 2010 01:49:18 -0700 (PDT)
    > Michele Simionato <> wrote:
    > >
    > > Notice that optparse is basically useless in the use case Tim is
    > > considering (positional arguments) since it only manages options.

    >
    > By the way, could you stop naming these "optional arguments", since
    > positional arguments can be optional as well? It is confusing :)
    >
    > Thanks
    >
    > Antoine.
    >
    >
     
    J. Cliff Dyer, Jun 2, 2010
    #10
  11. On Jun 2, 6:37 am, Michele Simionato <>
    wrote:
    > With blatant immodesty, plac claims to be the easiest to use command
    > line arguments parser module in the Python world


    It seems I have to take that claim back. A few hours after the
    announce I was pointed out to http://pypi.python.org/pypi/CLIArgs
    which, I must concede, is even easier to use than plac. It seems
    everybody has written its own command line arguments parser!
     
    Michele Simionato, Jun 2, 2010
    #11
  12. Michele Simionato

    alex23 Guest

    Michele Simionato <> wrote:
    > It seems I have to take that claim back. A few hours after the
    > announce I was pointed out tohttp://pypi.python.org/pypi/CLIArgs
    > which, I must concede, is even easier to use than plac. It seems
    > everybody has written its own command line arguments parser!


    I think I still find opterator[1] to be simpler and clearer. No magic
    global variables, no spooky behaviour with the main function, just a
    decorator and docstring.

    1: http://github.com/buchuki/opterator
     
    alex23, Jun 3, 2010
    #12
  13. On Jun 2, 6:37 am, Michele Simionato <>
    wrote:
    > I would like to announce to the world the first public release of
    > plac:
    >
    >  http://pypi.python.org/pypi/plac


    The second release is out. I have added the recognition of keyword
    arguments, improved the formatting of the help message, and added many
    tests.
     
    Michele Simionato, Jun 4, 2010
    #13
  14. Michele Simionato

    Kenny Meyer Guest

    On Jun 2, 12:37 am, Michele Simionato <>
    wrote:
    > I would like to announce to the world the first public release of
    > plac:
    >
    >  http://pypi.python.org/pypi/plac
    >
    > Plac is a wrapper over argparse and works in all versions of
    > Python starting from Python 2.3 up to Python 3.1.
    >
    > With blatant immodesty, plac claims to be the easiest to use command
    > line arguments parser module in the Python world. Its goal is to
    > reduce the
    > learning curve of argparse from hours to minutes. It does so by
    > removing the need to build a command line arguments parser by hand:
    > actually it is smart enough to infer the parser from function
    > annotations.
    >
    > Here is a simple example (in Python 3) to wet your appetite:
    >
    > $ cat example.py
    > def main(arg: "required argument"):
    >     "do something with arg"
    >     print('Got %s' % arg)
    >
    > if __name__ == '__main__':
    >     import plac; plac.call(main) # passes sys.argv[1:] to main
    >
    > $ python example.py -h
    > usage: example.py [-h] arg
    >
    > do something with arg
    >
    > positional arguments:
    >   arg         required argument
    >
    > optional arguments:
    >   -h, --help  show this help message and exit
    >
    > $ python example.py
    > usage: example.py [-h] arg
    > example.py: error: too few arguments
    >
    > $  python example.py arg
    > Got arg
    >
    > $  python example.py arg1 arg2
    > usage: example.py [-h] arg
    > example.py: error: unrecognized arguments: arg2
    >
    > You can find in the documentation a lot of other simple and not so
    > simple
    > examples:
    >
    >  http://micheles.googlecode.com/hg/plac/doc/plac.html
    >
    > Enjoy!
    >
    >              Michele Simionato
    >
    > P.S. answering an unspoken question: yes, we really needed yet
    > another
    > command line arguments parser! ;)


    I like this approach to command-line argument parsing! Thanks for
    sharing your work.
     
    Kenny Meyer, Jun 5, 2010
    #14
  15. Michele Simionato

    Anjum Naseer Guest

    You may be interested in a little Python module I wrote to make handling of command line arguments even easier (open source and free to use) - http://freshmeat.net/projects/commando

    > On Wednesday, June 02, 2010 12:37 AM Michele Simionato wrote:


    > I would like to announce to the world the first public release of
    > plac:
    >
    > http://pypi.python.org/pypi/plac
    >
    > Plac is a wrapper over argparse and works in all versions of
    > Python starting from Python 2.3 up to Python 3.1.
    >
    > With blatant immodesty, plac claims to be the easiest to use command
    > line arguments parser module in the Python world. Its goal is to
    > reduce the
    > learning curve of argparse from hours to minutes. It does so by
    > removing the need to build a command line arguments parser by hand:
    > actually it is smart enough to infer the parser from function
    > annotations.
    >
    > Here is a simple example (in Python 3) to wet your appetite:
    >
    > $ cat example.py
    > def main(arg: "required argument"):
    > "do something with arg"
    > print('Got %s' % arg)
    >
    > if __name__ == '__main__':
    > import plac; plac.call(main) # passes sys.argv[1:] to main
    >
    > $ python example.py -h
    > usage: example.py [-h] arg
    >
    > do something with arg
    >
    > positional arguments:
    > arg required argument
    >
    > optional arguments:
    > -h, --help show this help message and exit
    >
    >
    > $ python example.py
    > usage: example.py [-h] arg
    > example.py: error: too few arguments
    >
    > $ python example.py arg
    > Got arg
    >
    > $ python example.py arg1 arg2
    > usage: example.py [-h] arg
    > example.py: error: unrecognized arguments: arg2
    >
    > You can find in the documentation a lot of other simple and not so
    > simple
    > examples:
    >
    > http://micheles.googlecode.com/hg/plac/doc/plac.html
    >
    >
    > Enjoy!
    >
    > Michele Simionato
    >
    > P.S. answering an unspoken question: yes, we really needed yet
    > another
    > command line arguments parser! ;)



    >> On Wednesday, June 02, 2010 4:28 AM Tim Golden wrote:


    >> On 02/06/2010 05:37, Michele Simionato wrote:
    >>
    >> I like it. I am a constant user of the
    >>
    >> def main (a, b=1, c=2):
    >>
    >> if __name__ == '__main__':
    >> main (*sys.argv[1:])
    >>
    >> pattern, which provides a minimally semi-self-documenting
    >> approach for positional args, but I have always found the existing
    >> offerings just a little too much work to bother with.
    >> I will give plac a run and see how it behaves.
    >>
    >> Thanks
    >>
    >> TJG



    >>> On Wednesday, June 02, 2010 4:43 AM Paul Rubin wrote:


    >>> Tim Golden <> writes:
    >>>
    >>> After using optparse a couple of times I got the hang of it. Maybe its
    >>> docs could be organized a bit better, but it does the obvious things
    >>> conveniently once you have figured it out a bit.



    >>>> On Wednesday, June 02, 2010 4:49 AM Michele Simionato wrote:


    >>>> Notice that optparse is basically useless in the use case Tim is
    >>>> considering (positional arguments) since it only manages options.



    >>>>> On Wednesday, June 02, 2010 4:52 AM Jean-Michel Pichavant wrote:


    >>>>> Michele Simionato wrote:
    >>>>> Thanks for participating.
    >>>>>
    >>>>> JM



    >>>>>> On Wednesday, June 02, 2010 5:01 AM Stefan Behnel wrote:


    >>>>>> Paul Rubin, 02.06.2010 10:43:
    >>>>>>
    >>>>>> Same from here. I managed to talk a Java-drilled collegue of mine into
    >>>>>> writing a Python script for a little command line utility, but he needed a
    >>>>>> way to organise his argument extraction code when the number of arguments
    >>>>>> started to grow beyond two. I told him that there were two ways to do it:
    >>>>>> do it by hand or do it right. He took the right choice and I took him to
    >>>>>> the optparse docs, copied the first example into his code and we adapted it
    >>>>>> a little. He just loved the beauty of it.
    >>>>>>
    >>>>>> Stefan



    >>>>>>> On Wednesday, June 02, 2010 5:14 AM Michele Simionato wrote:


    >>>>>>> a
    >>>>>>> it
    >>>>>>>
    >>>>>>> Could you show plac to your friend? I would be curious to know what he
    >>>>>>> think.
    >>>>>>> Perhaps he would call out his judgment on optparse ;)



    >>>>>>>> On Wednesday, June 02, 2010 6:42 AM Antoine Pitrou wrote:


    >>>>>>>> By the way, could you stop naming these "optional arguments", since
    >>>>>>>> positional arguments can be optional as well? It is confusing :)
    >>>>>>>>
    >>>>>>>> Thanks
    >>>>>>>>
    >>>>>>>> Antoine.



    >>>>>>>>> On Wednesday, June 02, 2010 6:51 AM Tim Golden wrote:


    >>>>>>>>> On 02/06/2010 11:42, Antoine Pitrou wrote:
    >>>>>>>>>
    >>>>>>>>> The great thing with English is that you can use nouns as
    >>>>>>>>> adjectives without changing them, so you can say "option arguments"
    >>>>>>>>> and "position arguments" quite happily here :)
    >>>>>>>>>
    >>>>>>>>> But then you run into the fact that you are having semantic arguments
    >>>>>>>>> about argument semantics :(
    >>>>>>>>>
    >>>>>>>>> TJG



    >>>>>>>>>> On Wednesday, June 02, 2010 9:06 AM J. Cliff Dyer wrote:


    >>>>>>>>>> +1
    >>>>>>>>>>
    >>>>>>>>>> Options are options, arguments are arguments. An optional argument is
    >>>>>>>>>> not an option. It is an argument that can be left out.



    >>>>>>>>>>> On Wednesday, June 02, 2010 9:46 AM Michele Simionato wrote:


    >>>>>>>>>>> wrote:
    >>>>>>>>>>>
    >>>>>>>>>>> It seems I have to take that claim back. A few hours after the
    >>>>>>>>>>> announce I was pointed out to http://pypi.python.org/pypi/CLIArgs
    >>>>>>>>>>> which, I must concede, is even easier to use than plac. It seems
    >>>>>>>>>>> everybody has written its own command line arguments parser!



    >>>>>>>>>>>> On Wednesday, June 02, 2010 8:51 PM alex23 wrote:


    >>>>>>>>>>>> I think I still find opterator[1] to be simpler and clearer. No magic
    >>>>>>>>>>>> global variables, no spooky behaviour with the main function, just a
    >>>>>>>>>>>> decorator and docstring.
    >>>>>>>>>>>>
    >>>>>>>>>>>> 1: http://github.com/buchuki/opterator



    >>>>>>>>>>>>> On Thursday, June 03, 2010 11:28 PM Michele Simionato wrote:


    >>>>>>>>>>>>> wrote:
    >>>>>>>>>>>>>
    >>>>>>>>>>>>> The second release is out. I have added the recognition of keyword
    >>>>>>>>>>>>> arguments, improved the formatting of the help message, and added many
    >>>>>>>>>>>>> tests.



    >>>>>>>>>>>>>> On Friday, June 04, 2010 8:48 PM Kenny Meyer wrote:


    >>>>>>>>>>>>>> wrote:
    >>>>>>>>>>>>>>
    >>>>>>>>>>>>>> I like this approach to command-line argument parsing! Thanks for
    >>>>>>>>>>>>>> sharing your work.



    >>>>>>>>>>>>>> Submitted via EggHeadCafe
    >>>>>>>>>>>>>> WCF Generic DataContract object Serializer
    >>>>>>>>>>>>>> http://www.eggheadcafe.com/tutorial...f-generic-datacontract-object-serializer.aspx
     
    Anjum Naseer, Feb 6, 2011
    #15
    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. Ahmed Moustafa

    Parser for command line arguments?

    Ahmed Moustafa, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    390
    Ahmed Moustafa
    Aug 21, 2003
  2. Replies:
    4
    Views:
    1,716
    Terry Hancock
    Apr 23, 2005
  3. vijay
    Replies:
    8
    Views:
    713
  4. Hans-Peter Diettrich
    Replies:
    2
    Views:
    438
    Hans-Peter Diettrich
    Aug 22, 2008
  5. Michele Simionato

    plac 0.5 is out!

    Michele Simionato, Jun 20, 2010, in forum: Python
    Replies:
    5
    Views:
    224
    Andre Alexander Bell
    Jun 21, 2010
Loading...

Share This Page