mentby.com
Blog | Jobs | Help | Signup | Login

[Tutor] Arguments from the command line



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/argparse.html#module-argparse

Hugo


Hugo Arts Mon, 06 Sep 2010 09:17:59 -0700

import sys
print sys.argv

$hg commit "This is a commit name"
['C:\\hg.py', 'commit', 'This is a commit name']

--
Bob Gailer
919-636-4239
Chapel Hill NC


Bob Gailer Mon, 06 Sep 2010 09:42:49 -0700

In message <mailman.501.1283789339.29448.python-list*******>, Hugo Arts

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.
--  http://mail.python.org/mailman/listinfo/python-list


Lawrence D'Oliveiro Wed, 08 Sep 2010 17:43:02 -0700

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

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


Robert Kern Wed, 08 Sep 2010 20:27:00 -0700

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
        wlfraed*******/

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


Dennis Lee Bieber Wed, 08 Sep 2010 22:02:09 -0700

What mainline routine?

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
--  http://mail.python.org/mailman/listinfo/python-list


Steven D'Aprano Wed, 08 Sep 2010 22:06:06 -0700

In message <mailman.599.1284008342.29448.python-list*******>, Dennis Lee

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.
--  http://mail.python.org/mailman/listinfo/python-list


Lawrence D'Oliveiro Wed, 08 Sep 2010 22:55:51 -0700

Java uses a method defined as "public static void main(String[] args)"


There isn't, but command line argument passing is implementation-
dependent and is complicated by the ability to define callable sub-
programs in the same source file as the main program. The most general
method is to use ACCEPT statements. MicroFocus COBOL uses "ACCEPT ...
FROM ARGUMENT-NUMBER", AIX COBOL uses a special system call and ICL 2900
COBOL and IBM COBOL/400, where the command line uses function call
notation, map the command line arguments into a LINKAGE SECTION.

In short: this area of COBOL is a mess.

PL/I  specifies the main procedure with an OPTIONS(MAIN) clause and
declares the integer ARGC_ and pointer ARGV_ variables in it, which are
used like their C equivalents.
  

--
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |
--  http://mail.python.org/mailman/listinfo/python-list


Martin Gregorie Thu, 09 Sep 2010 03:18:07 -0700

[ ... ]

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.

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


Mel Wilson Thu, 09 Sep 2010 07:28:04 -0700

no BLOCKDATA?


Giacomo Boffi Thu, 09 Sep 2010 07:58:02 -0700

I think you mean COMMON.


Lawrence D'Oliveiro Fri, 10 Sep 2010 02:37:34 -0700

I think it could also read from switches. As in front-panel on/off switches.


Lawrence D'Oliveiro Fri, 10 Sep 2010 02:41:29 -0700

i meant BLOCKDATA


Giacomo Boffi Fri, 10 Sep 2010 03:33:52 -0700

It exists but I've only seen it used once... And I don't recall it
being "executable" in the same way as program/function/subroutine.
--
    Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed*******/


Dennis Lee Bieber Fri, 10 Sep 2010 09:22:17 -0700

BLOCKDATA is an initializer. The actual storage is allocated by COMMON.


Lawrence D'Oliveiro Fri, 10 Sep 2010 15:27:42 -0700

i simply meant that the BLOCKDATA statement can begin a subfile as
well as SUBROUTINE or FUNCTION
--
Sarò un'ingenua ma continuo a pensarla come prima, anche se
probabilmente i fatti mi smentiscono.  -- Francy, in IHC
-- http://mail.python.org/mailman/listinfo/python-list


Giacomo Boffi Mon, 13 Sep 2010 08:26:01 -0700



Related Topics

Post a Comment