Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > plac, the easiest command line arguments parser in the world

Reply
Thread Tools

plac, the easiest command line arguments parser in the world

 
 
Michele Simionato
Guest
Posts: n/a
 
      06-02-2010
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!
 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      06-02-2010
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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      06-02-2010
Tim Golden <(E-Mail Removed)> 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.
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      06-02-2010
On Jun 2, 10:43*am, Paul Rubin <(E-Mail Removed)> wrote:
> Tim Golden <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      06-02-2010
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
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      06-02-2010
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

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      06-02-2010
On Jun 2, 11:01*am, Stefan Behnel <(E-Mail Removed)> 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

 
Reply With Quote
 
Antoine Pitrou
Guest
Posts: n/a
 
      06-02-2010
On Wed, 2 Jun 2010 01:49:18 -0700 (PDT)
Michele Simionato <(E-Mail Removed)> 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.


 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      06-02-2010
On 02/06/2010 11:42, Antoine Pitrou wrote:
> On Wed, 2 Jun 2010 01:49:18 -0700 (PDT)
> Michele Simionato<(E-Mail Removed)> 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
 
Reply With Quote
 
J. Cliff Dyer
Guest
Posts: n/a
 
      06-02-2010
+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 <(E-Mail Removed)> 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Documentation on command line arguments to perl Peter Kay Perl 1 05-18-2004 01:27 AM
Parser for command line arguments? Ahmed Moustafa Java 0 08-21-2003 05:21 AM



Advertisments