Re: [Tutor] Arguments from the command line

Discussion in 'Python' started by Hugo Arts, Sep 6, 2010.

  1. Hugo Arts

    Hugo Arts Guest

    On Mon, Sep 6, 2010 at 5:48 PM, aug dawg <> 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/argparse.html#module-argparse

    Hugo
     
    Hugo Arts, Sep 6, 2010
    #1
    1. Advertising

  2. In message <>, 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.
     
    Lawrence D'Oliveiro, Sep 9, 2010
    #2
    1. Advertising

  3. Hugo Arts

    Robert Kern Guest

    On 9/8/10 7:38 PM, Lawrence D'Oliveiro wrote:
    > In message<>, 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
     
    Robert Kern, Sep 9, 2010
    #3
  4. On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro wrote:

    > In message <>, 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
     
    Steven D'Aprano, Sep 9, 2010
    #4
  5. On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro
    <_zealand> declaimed the following in
    gmane.comp.python.general:

    > In message <>, 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.

    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://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Sep 9, 2010
    #5
  6. In message <>, 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.
     
    Lawrence D'Oliveiro, Sep 9, 2010
    #6
  7. Hugo Arts

    Mel Guest

    Dennis Lee Bieber wrote:

    > On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro
    > <_zealand> declaimed the following in
    > gmane.comp.python.general:
    >
    >> In message <>, 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.
     
    Mel, Sep 9, 2010
    #7
  8. Dennis Lee Bieber <> 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?
     
    Giacomo Boffi, Sep 9, 2010
    #8
  9. In message <i6aqk5$roo$>, 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.
     
    Lawrence D'Oliveiro, Sep 10, 2010
    #9
  10. Lawrence D'Oliveiro <_zealand> writes:

    > In message <>, Giacomo Boffi wrote:
    >
    >> Dennis Lee Bieber <> 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
     
    Giacomo Boffi, Sep 10, 2010
    #10
  11. In message <>, Giacomo Boffi wrote:

    > Lawrence D'Oliveiro <_zealand> writes:
    >
    >> In message <>, Giacomo Boffi wrote:
    >>
    >>> no BLOCKDATA?

    >>
    >> I think you mean COMMON.

    >
    > i meant BLOCKDATA


    BLOCKDATA is an initializer. The actual storage is allocated by COMMON.
     
    Lawrence D'Oliveiro, Sep 10, 2010
    #11
  12. Dennis Lee Bieber <> writes:

    > On Fri, 10 Sep 2010 12:25:17 +0200, Giacomo Boffi
    > <> declaimed the following in
    > gmane.comp.python.general:
    >
    >> Lawrence D'Oliveiro <_zealand> writes:
    >>
    >> > In message <>, Giacomo Boffi wrote:
    >> >
    >> >> Dennis Lee Bieber <> 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

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


    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
     
    Giacomo Boffi, Sep 13, 2010
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Kay
    Replies:
    1
    Views:
    503
    Jürgen Exner
    May 18, 2004
  2. Ahmed Moustafa

    Parser for command line arguments?

    Ahmed Moustafa, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    398
    Ahmed Moustafa
    Aug 21, 2003
  3. SC
    Replies:
    2
    Views:
    675
    Michiel Salters
    May 5, 2004
  4. Ashe Corven

    command line arguments

    Ashe Corven, May 8, 2004, in forum: C++
    Replies:
    3
    Views:
    540
    Francis Glassborow
    May 8, 2004
  5. Replies:
    4
    Views:
    1,765
    Terry Hancock
    Apr 23, 2005
Loading...

Share This Page