Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7400 > unrolled thread

Re: Question About Command line arguments

Started byDennis <daodennis@gmail.com>
First post2011-06-10 13:33 -0700
Last post2011-06-10 13:33 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Question About Command line arguments Dennis <daodennis@gmail.com> - 2011-06-10 13:33 -0700

#7400 — Re: Question About Command line arguments

FromDennis <daodennis@gmail.com>
Date2011-06-10 13:33 -0700
SubjectRe: Question About Command line arguments
Message-ID<mailman.106.1307738018.11593.python-list@python.org>
On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips
<mark@phillipsmarketing.biz> wrote:
\
>
> Kurt,
>
> How does one write a main method to handle both command line args and stdin
> args?

Here is what I came up with:

The one weird thing, the line from above didn't seem to work so I changed it
if os.isatty(sys.stdin):

to this:

if not os.isatty(sys.stdin.fileno()):

Below 3 tests, with stdin redirection, then with one argument, then
with both stdin redirection and one argument, the results are at the
very bottom.

$ cat ./argvtest.py; echo "fred" | ./argvtest.py ; ./argvtest.py
"alice"; echo "fred" | ./argvtest.py "bob"
#!/usr/bin/python


import os
import sys

def main():
        #This checks to see if stdin is a not tty and > 0
        if not os.isatty(sys.stdin.fileno()):
                arg = sys.stdin.read()
                print arg

        elif len(sys.argv[1:]) > 0:

        # if the length of the first argument is > 0
        #[1:] strip the first string beacause it is the name of the script
                arg = sys.argv[1:]
                print arg

if __name__ == "__main__":
        main()
fred

['alice']
fred





>
> Thanks,
>
> Mark
>

Welcome,

Dennis O.

> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web