Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: [Tutor] Arguments from the command line

Reply
Thread Tools

Re: [Tutor] Arguments from the command line

 
 
Hugo Arts
Guest
Posts: n/a
 
      09-06-2010
On Mon, Sep 6, 2010 at 5:48 PM, aug dawg <(E-Mail Removed)> wrote:
> I've seen Python programs that can be activated from the command line. For
> example:
> hg
>
> This displays a list of commands for the Mercurial revision control system.
> But another command is this:
> hg commit "This is a commit name"
> Mercurial is written in Python. I know that commit is a function that
> commits to a repo, but what command does the program use in order to get the
> commit name, like "This is a commit name" (This would make a commit with
> "This is a commit name" as the commit name)
>


sys.argv is a list of all arguments from the command line. However,
you'll rarely deal with it directly, there's various modules that deal
with handling arguments. I believe the current one is argparse:
http://docs.python.org/library/argpa...odule-argparse

Hugo
 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-09-2010
In message <(E-Mail Removed)>, Hugo Arts
wrote:

> sys.argv is a list of all arguments from the command line ...


Interesting that Python didn’t bother to mimic the underlying POSIX
convention of passing the command line as arguments to the mainline routine.

I always felt it was more useful to have command arguments directly
accessible as globals, rather than having to pass them from the mainline.
 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      09-09-2010
On 9/8/10 7:38 PM, Lawrence D'Oliveiro wrote:
> In message<(E-Mail Removed)>, Hugo Arts
> wrote:
>
>> sys.argv is a list of all arguments from the command line ...

>
> Interesting that Python didn’t bother to mimic the underlying POSIX
> convention of passing the command line as arguments to the mainline routine.


There *is* no mainline routine in Python.

--
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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-09-2010
On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro wrote:

> In message <(E-Mail Removed)>, Hugo
> Arts wrote:
>
>> sys.argv is a list of all arguments from the command line ...

>
> Interesting that Python didn’t bother to mimic the underlying POSIX
> convention of passing the command line as arguments to the mainline
> routine.


What mainline routine?


> I always felt it was more useful to have command arguments directly
> accessible as globals, rather than having to pass them from the
> mainline.


http://c2.com/cgi/wiki?GlobalVariablesAreBad

That's why we have namespaces. If you need the command line arguments,
you just import sys and you have access to them.



--
Steven
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-09-2010
On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro
<(E-Mail Removed)_zealand> declaimed the following in
gmane.comp.python.general:

> In message <(E-Mail Removed)>, Hugo Arts
> wrote:
>
> > sys.argv is a list of all arguments from the command line ...

>
> Interesting that Python didnt bother to mimic the underlying POSIX
> convention of passing the command line as arguments to the mainline routine.
>

What "mainline routine"... The only programming language(s) I've
ever used that requires there be something called "main" in order to
start a program is the C/C++ family.

FORTRAN just differentiates by having the main file start with
PROGRAM random_name
whereas subfiles are all either (or both)
SUBROUTINE another_name(args)
FUNCTION that_other_name(args)

My college COBOL never used multifile assignments, so I'm not sure
if there was a difference between main and linked modules.

Ada -- defined by the body file used to start the build sequence...

Original Pascal did not make use of linkable subfiles, and the only
defined "arguments" on the main procedure were the logical names of I/O
streams.

None of them have command line arguments "passed" in to some
starting point -- all had to use some runtime library function to ask
for the command line contents.
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-09-2010
In message <(E-Mail Removed)>, Dennis Lee
Bieber wrote:

> None of them have command line arguments "passed" in to some
> starting point -- all had to use some runtime library function to ask
> for the command line contents.


It’s always a language-specific routine, since at the underlying POSIX level
(exposed by C), the command line is passed as arguments to the mainline
routine.
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      09-09-2010
Dennis Lee Bieber wrote:

> On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro
> <(E-Mail Removed)_zealand> declaimed the following in
> gmane.comp.python.general:
>
>> In message <(E-Mail Removed)>, Hugo
>> Arts wrote:
>>
>> > sys.argv is a list of all arguments from the command line ...

>>
>> Interesting that Python didn’t bother to mimic the underlying POSIX
>> convention of passing the command line as arguments to the mainline
>> routine.
>>

> What "mainline routine"... The only programming language(s) I've
> ever used that requires there be something called "main" in order to
> start a program is the C/C++ family.

[ ... ]
> My college COBOL never used multifile assignments, so I'm not sure
> if there was a difference between main and linked modules.


Historical COBOL had a PROCEDURE DIVISION which marked the start of
execution. But historical COBOL didn't pass parameters anyway. You read
your optional arguments from a file, or accepted a few from an input device.

I don't know PL/I generally, but with Multics PL/I any externally accessible
procedure could be called from the command line:

any_program$ea_proc a b c

passing (for example) 'a', 'b', and 'c' as parameters. If the parameters
were declared as variable-length character strings:

ea_proc: proc (x, y, z);
dcl (x, y, z) char (*);
...

this would even work. Illustrating that the command-line-parameter-passing
question is a deal with the operating system at least as much as it's a
language issue. Posix aren't the only O/S.

Mel.

 
Reply With Quote
 
Giacomo Boffi
Guest
Posts: n/a
 
      09-09-2010
Dennis Lee Bieber <(E-Mail Removed)> writes:

> FORTRAN just differentiates by having the main file start with
> PROGRAM random_name
> whereas subfiles are all either (or both)
> SUBROUTINE another_name(args)
> FUNCTION that_other_name(args)


no BLOCKDATA?
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-10-2010
In message <i6aqk5$roo$(E-Mail Removed)>, Mel wrote:

> But historical COBOL didn't pass parameters anyway. You read
> your optional arguments from a file, or accepted a few from an input
> device.


I think it could also read from switches. As in front-panel on/off switches.
 
Reply With Quote
 
Giacomo Boffi
Guest
Posts: n/a
 
      09-10-2010
Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:

> In message <(E-Mail Removed)>, Giacomo Boffi wrote:
>
>> Dennis Lee Bieber <(E-Mail Removed)> writes:
>>
>>> FORTRAN just differentiates by having the main file start with
>>> PROGRAM random_name
>>> whereas subfiles are all either (or both)
>>> SUBROUTINE another_name(args)
>>> FUNCTION that_other_name(args)

>>
>> no BLOCKDATA?

>
> I think you mean COMMON.


i meant BLOCKDATA
 
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
command line arguments Ashe Corven C++ 3 05-08-2004 06:56 PM
How to hide command line arguments in a windows application SC C++ 2 05-05-2004 10:06 AM
Parser for command line arguments? Ahmed Moustafa Java 0 08-21-2003 05:21 AM



Advertisments