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

command line utility for cups

17 views
Skip to first unread message

Brian Oney

unread,
Jun 20, 2018, 5:28:54 AM6/20/18
to
Dear all,

I am having trouble with argparse. I am trying to translate the following line to a sleek python
script:

lpr -o media=legal -o sides=two-sided-long-edge filename

Now where I am.

import argparse
parser = argparse.ArgumentParser(description='Print stuff with cups')
parser.add_argument('--printer', '-p',
                    help='Use this printer, try running: lpstat -a')
parser.add_argument('--options', '-o', 
                    help='Options for this printer, try running: \
                    lpoptions -p PRINTER -l')

parser.parse_known_args(['-o', 'sides=one-sided', '-o', 'test=crap'])
Namespace(options='test=crap', printer=None))

How should I deal with multiple options being fed into my script?

Thanks!

Cheers,
Brian

Peter Otten

unread,
Jun 20, 2018, 6:21:24 AM6/20/18
to
Try action="append". You may also provide a type=split_func argument to
split the key=value pairs:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("-o", action="append", type=lambda p: p.split("=",
1))
_AppendAction(option_strings=['-o'], dest='o', nargs=None, const=None,
default=None, type=<function <lambda> at 0x7feef70a8510>, choices=None,
help=None, metavar=None)
>>> parser.parse_args(["-o", "a=b", "-o", "c=d"])
Namespace(o=[['a', 'b'], ['c', 'd']])


George Fischhof

unread,
Jun 20, 2018, 6:37:03 AM6/20/18
to
Peter Otten <__pet...@web.de> ezt írta (időpont: 2018. jún. 20., Sze
12:22):
> --
> https://mail.python.org/mailman/listinfo/python-list



Hi,
You can also try click library from pypi, that is a very good command line
stuff.

George

>
>

Brian Oney

unread,
Jun 20, 2018, 6:46:51 AM6/20/18
to
Thanks Peter!

That's pretty slick.

I will get it working for sure now.

Regards,
Brian

Brian Oney

unread,
Jun 20, 2018, 7:41:07 AM6/20/18
to
On Wed, 2018-06-20 at 12:36 +0200, George Fischhof wrote:
> Hi,
> You can also try click library from pypi, that is a very good command line
> stuff.
>
> George

Thank you for the tip. I am away of click and it's awesomeness, but am hesitant because it's not apart of stdlib. I have gotten bitten too often.

0 new messages