Python has two built-in modules to handle command-line arguments, one is getopt, and the Deep inPython is also mentioned in the book, can only handle command-line parameters, the other is Optparse, it is powerful, and easy to use, can easily generate standard, compliantComma
When you are working on a project recently, you need to use Golang to invoke the command line in the operating system to execute a shell command or call a third-party program, which naturally uses the Exec.command of Golang.
But if you use native exec directly. command creates a lot of duplicate code, searches the web
Command-line execution under PHP
The following are the options parameters for the command-line mode provided by the PHP binaries (that is, the php.exe program), which you can query at any time by using the php-h command.
usage:php [Options] [-f]
The following are the options parameters for the command-line mode provided by the PHP binaries (that is, the Php.exe program), which you can query at any time by using the Php-h command.
usage:php [Options] [-f] PHP [Options]-R PHP [Options] [--args ...]-S Display colour syntax highlighted source.-W Display source with stripped comments and whitespace.-
to use PHP for shell scripts. You can write a script and start with #!/usr/bin/php in the first line, followed by the normal PHP code that was included with the PHP start and end tags, and then set the correct run properties for the file (for example: chmod +x test). This method allows the file to be executed directly like a shell script or a PERL script.
#!/usr/bin/php
If you change the file name to test and are placed in the current directory, you
.
#!/usr/bin/php
Assuming that the file name is test and placed in the current directory, we can do the following:
$ chmod 755 test$./test-h--Fooarray (4) { [0]=> string (6) "./test" [1]=> string (2) "-H" [2] = = String (2) "--" [3]=> string (3) "Foo"}
As you can see, the script will still work correctly when you send the arguments that begin with-to the script.
------------------------------------------------------------------------------
. args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin
The cli sapi module has the following th
In many applications run with command line parameters, in fact, these command-line parameters in the C language program can also be implemented, flexible use of command-line parameters to deal with can effectively improve the oper
Docopt is a tool for parsing command-line arguments, and you don't need to worry about it when you want to attach parameters to a Python program. The following article mainly introduces the docopt of command-line parameter parsing tool in Python, introduces the very detailed
($argv); '-husage:php [options] [-f]
In addition, we have another way to use PHP for Shell scripting. You can write a script and start with #!/usr/bin/php in the first line, followed by the normal PHP code that was included with the PHP start and end tags, and then set the correct run properties for the file. This method allows the file to be executed directly like a shell script or a PERL script.
command will not run PHP code, but only show instructions for using PHP command-line mode: $ Php-r ' var_dump ($argv); '-husage:php [options] [-f]
In addition, we have another way to use PHP for Shell scripting. You can write a script and start with #!/usr/bin/php in the first line, followed by the normal
, you can get a good help information. But giving other parameters (such as Foo) produces an error.
3. Position parameters
First, give an example:
Import Argparseparser = Argparse. Argumentparser () parser.add_argument ("echo") args = Parser.parse_args () print Args.echo
Operation Result:
$ Python prog.pyusage:prog.py [-h] echoprog.py:error:the following arguments is required:echo$ python prog.py--helpu sage:prog.py [-h] echopositional arguments:ec
= optarg;Printf ("Our love is % s/n", optarg );Break;}}Return 0;}
Running result: $./opt_parse_demo-nMy name is lyong.$./Opt_parse_demo-GHer name is xxiong.$./Opt_parse_demo-l foreverOur love is forever$./Opt_parse_demo-NGL foreverMy name is lyong.Her name is xxiong.Our love is forever
6. Change the output behavior of getopt () on the parameter information of the wrong command line.
Incorrect calling is in
When you use Python for scripting, it is often necessary to invoke the Python script module in the form of a terminal interaction. At this timeSupport for providing the underlying command-line parameters in a Python module is essential. So, in Python, how do we implement command-line
Question Description: How do you deal with command-line options such as Tail-n Access.log in a Linux shell?
In bash, command line arguments can be handled in three ways, each with its own scenario.
1, direct processing, in turn to the $1,$2,..., $n, respectively, hand-handle
para_1 string.ARGV[2] points to the parameter para_2 string.void Main (int argc, char *argv[])Char *argv[]: argv is an array of pointers, the number of his elements is argc, storing pointers to each parameter,His first element is argv[0] The executable file name (including path eg: "F:/vc/ex1/debug/ex1.exe") generated for the compilation,Starting with two elements (Argv[1]),is each parameter int argc represents the size of the argv, which is the actual number of parameters +1,Where +1 is becaus
shows the benefits of using the Argparse, you did nothing, but got a good help information.3), we do not need to set the--help parameters, you can get a good help information. But giving other parameters (such as Foo) produces an error.
3. Position parameters
First, give an example:
Import Argparseparser = Argparse. Argumentparser () parser.add_argument ("echo") args = Parser.parse_args () print Args.echo
Operation Result:
$ Python prog.pyusage:prog.py [-h] echoprog.py:error:the following
both are command subcommand [arguments]. arguments is sometimes optional. Therefore, the first step of the execute method is to obtain the subcommand to determine the subsequent tasks.
1 try:2 subcommand = self.argv[1]3 except IndexError:4 subcommand = 'help' # Display help if no arg
When writing a shell program, you often need to handle command-line arguments, and this article describes the command-line approach under bash.Options and Parameters:Like the following command
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.