sys module - argv, but no argc ?? 
Author Message
 sys module - argv, but no argc ??

I've imported the sys module, and I'm SHOCKED that there is no way to
access the argument count.

The docs talk about accessing argv, but nothing about the argument
count.
Was something omitted from the docs ?

How do python users get around this ?

thanks

Tony



Tue, 18 Jan 2005 14:53:26 GMT  
 sys module - argv, but no argc ??

Quote:

> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

> The docs talk about accessing argv, but nothing about the argument
> count.

No need.  Use:

    len( sys.argv )

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax



Tue, 18 Jan 2005 15:00:56 GMT  
 sys module - argv, but no argc ??

Quote:

>I've imported the sys module, and I'm SHOCKED that there is no way to
>access the argument count.

>The docs talk about accessing argv, but nothing about the argument
>count.
>Was something omitted from the docs ?

>How do Python users get around this ?

len(sys.argv)

--titus



Tue, 18 Jan 2005 15:29:26 GMT  
 sys module - argv, but no argc ??

Quote:

> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

> The docs talk about accessing argv, but nothing about the argument
> count.
> Was something omitted from the docs ?

> How do Python users get around this ?

> thanks

> Tony

Try this little snippet and see for yourself:

from sys import *

argc = len(argv)
print "argv = %s       argc = %s" % (argv, argc)

--
------------------------------------------------------------------------------
Tim Daneliuk



Tue, 18 Jan 2005 15:30:02 GMT  
 sys module - argv, but no argc ??
On 2/8/2002 7:53, in article

Quote:

> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

That's because you're thinking like a C programmer ;-)

-a-list-is-not-a-piece-of-string-ly y'rs,

Jonathan



Tue, 18 Jan 2005 16:44:05 GMT  
 sys module - argv, but no argc ??

Quote:
> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

len(argv)

argv is a Python list - like Python lists generally, you can resize
it, find out its current size, and do all sorts of things to it.

Hamish Lawson



Tue, 18 Jan 2005 21:52:51 GMT  
 sys module - argv, but no argc ??

Quote:
> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

> The docs talk about accessing argv, but nothing about the argument
> count.
> Was something omitted from the docs ?

> How do Python users get around this ?

Tony:

Now you've had at least five good answers, has your shock lessened to the
degree that you might start regarding Python as useful?

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------



Wed, 19 Jan 2005 00:04:30 GMT  
 sys module - argv, but no argc ??
But beware that sys.argv includes the script as the first item
(sys.argv[0]), so if you do:

        len(sys.argv)

you will actually get the number of args passed + 1.

Alternately, just do

        len(sys.argv) - 1

Gabe

Quote:
-----Original Message-----

Sent: Friday, August 02, 2002 12:01 AM
To: Tony C


Subject: Re: sys module - argv, but no argc ??


> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

> The docs talk about accessing argv, but nothing about the argument
> count.

No need.  Use:

    len( sys.argv )

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax

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



Tue, 18 Jan 2005 23:25:54 GMT  
 sys module - argv, but no argc ??


Quote:
>But beware that sys.argv includes the script as the first item

(sys.argv[0]),

So does argv in C

Quote:
> so if you do: len(sys.argv) you will actually get the number of args

passed + 1.

Which is also true of argc in C.  Python is here mimicking C, which is
what the O.P. wanted.  Standard loop:

for (i = 1 /*not 0*/, i < argc, i++) process(arg[i]);

In Python:

for i in argv[1:]: process(i)

Terry J. Reedy



Wed, 19 Jan 2005 02:32:10 GMT  
 sys module - argv, but no argc ??
Yes indeed. I felt I needed to comment, however, since one of the
respondents told the OP that len(sys.argv) returns the number of args
passed, and that's not true if you consider args to be those things
passed in addition to the calling script/process.

Gabe

Quote:
-----Original Message-----

Sent: Friday, August 02, 2002 11:32 AM

Subject: Re: sys module - argv, but no argc ??



>But beware that sys.argv includes the script as the first item
(sys.argv[0]),

So does argv in C

> so if you do: len(sys.argv) you will actually get the number of args
passed + 1.

Which is also true of argc in C.  Python is here mimicking C, which is
what the O.P. wanted.  Standard loop:

for (i = 1 /*not 0*/, i < argc, i++) process(arg[i]);

In Python:

for i in argv[1:]: process(i)

Terry J. Reedy

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



Wed, 19 Jan 2005 03:13:17 GMT  
 sys module - argv, but no argc ??

Quote:

> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

There isn't?  I never noticed that...

I guess I just never needed it...

Take care,
Jay O'Connor
http://www.r4h.org/r4hsoftware



Wed, 19 Jan 2005 05:35:48 GMT  
 sys module - argv, but no argc ??

Quote:

> On 2/8/2002 7:53, in article


> > I've imported the sys module, and I'm SHOCKED that there is no way to
> > access the argument count.

> That's because you're thinking like a C programmer ;-)

I think so, since it's so wasy to do

for arg in sys.argv:
    .....

the need to have argc around to iterate over the array is not needed

for (i = 0; i << argc; i) {
   arg = argv[i]

Quote:
}



Wed, 19 Jan 2005 05:38:19 GMT  
 sys module - argv, but no argc ??
[Tony C]

Quote:
> I've imported the sys module, and I'm SHOCKED that there is no way to
> access the argument count.

Indeed, it would be shocking if there was no way.  Have more confidence
in Python design, and you will soon be happier! :-)

Quote:
> The docs talk about accessing argv, but nothing about the argument count.
> Was something omitted from the docs ?

The way to get the length of a list is surely documented along with lists,
but not repeated for each and every list Python offers, that would soon
become tedious.

Quote:
> How do Python users get around this ?

Merely by using len(sys.argv).

--
Fran?ois Pinard   http://www.iro.umontreal.ca/~pinard



Wed, 19 Jan 2005 07:24:58 GMT  
 sys module - argv, but no argc ??


Yes indeed. I felt I needed to comment, however, since one of the
respondents told the OP that len(sys.argv) returns the number of args
passed, and that's not true if you consider args to be those things
passed in addition to the calling script/process.

Me: I wasn't contradicting your warning, just the implication (perhaps
not meant by you) that Python was doing anything extra 'wrong' or
'misleading'.  C programmers also need the warning.  K&R (The C
Programming Language) first call argc "the number of command-line
arguments" (p.110) and only later explain (next page) that it is
actually one more.


Quote:
>But beware that sys.argv includes the script as the first item

(sys.argv[0]),

So does argv in C

Quote:
> so if you do: len(sys.argv) you will actually get the number of args

passed + 1.

Which is also true of argc in C.  Python is here mimicking C, which is
what the O.P. wanted.



Wed, 19 Jan 2005 09:52:10 GMT  
 sys module - argv, but no argc ??
On Fri, 02 Aug 2002 00:00:56 -0700, James J. Besemer Wrote in
Steve Ballmers hair grease:

Quote:


>> I've imported the sys module, and I'm SHOCKED that there is no way to
>> access the argument count.

>> The docs talk about accessing argv, but nothing about the argument
>> count.

> No need.  Use:

>     len( sys.argv )

Being the newbie that I am, I just so happened to be working on this
very thing.

I am sure that this isn't the most elegant way of parsing the command
line, but it works well. And no index out of bounds errors, thus no
need for argc.

# Check to see if command line has any perimeters. -s is newserver,
# -g is group. Range indexes manually, and allows the disregarding of
# argv[0] the way a slice would.

import string
import sys

for index in range(1,len(sys.argv)):
    str = string.upper(sys.argv[index])
    if str == "-G":
        newsgroup = sys.argv[index+1]
    elif str == "-S":
        nntpserver = sys.argv[index+1]

Cheers,

Mathew

--
TuxTrax   (n.) An invincible, all terrain, Linux driven armored {*filter*}
vehicle that can take as much fire as it gives ;-)

Yes, I am a Penguin cult high priest. Flipper readings upon request.




Wed, 19 Jan 2005 19:00:39 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Specifiying a module from sys.argv?

2. Chosing between sys.argv and sys.stdin

3. Crazy ARGC/ARGV behavior

4. ArgC/ArgV - Command Line Parsing

5. Forth argv and argc

6. Syntax Question: ARGV/ARGC for Eiffel

7. Problem w ruby_options(argc, argv) in embedded Ruby app

8. Accessing argc,argv,environ from a ruby extension?

9. Re^2: argc and *argv in Modula-2 ?

10. argc and *argv in Modula-2 ?

11. argc and *argv in Modula-2 ?

12. Argc And *Argv In Modula-

 

 
Powered by phpBB® Forum Software