Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7398
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Subject | Re: Question About Command line arguments |
| Date | 2011-06-10 14:27 -0500 |
| Organization | The Church of Last Thursday |
| References | <BANLkTi==QHd+5C=xf+2Re1Mh4V6Hk972hg@mail.gmail.com> <4DF25738.2020704@mrabarnett.plus.com> <BANLkTimHfZK=FsbSs4BOX8dU=mt2bVGV9A@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.103.1307734048.11593.python-list@python.org> (permalink) |
On 6/10/11 12:58 PM, Mark Phillips wrote:
> On Fri, Jun 10, 2011 at 10:41 AM, MRAB <python@mrabarnett.plus.com
> <mailto:python@mrabarnett.plus.com>> wrote:
>
> On 10/06/2011 18:21, Mark Phillips wrote:
>
> I have a script that processes command line arguments
>
> def main(argv=None):
> syslog.syslog("Sparkler stared processing")
> if argv is None:
> argv = sys.argv
> if len(argv) != 2:
> syslog.syslog(usage())
> else:
> r = parseMsg(sys.argv[1])
> syslog.syslog(r)
> return 0
>
> if __name__ == "__main__":
> sys.exit(main())
>
> When I run "python myscript fred" it works as expected - the argument
> fred is processed in parseMsg as sys.arv[1]
>
> When I run "echo fred | python myscript" the script thinks there are no
> arguments, so it prints out the usage statement.
>
> Is the problem with the echo command, or how I wrote my script?
>
> In the second case, there aren't any arguments. The echo command is
> writing "fred" to its standard output, which is attached to your
> script's standard input.
>
> How do I write my script so it picks up argument from the output of commands
> that pipe input into my script?
You may want to just use the appropriate shell syntax instead:
$ python myscript `echo fred`
--
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question About Command line arguments Robert Kern <robert.kern@gmail.com> - 2011-06-10 14:27 -0500
csiph-web