Command Line Parameter Parsing

Source: Internet
Author: User

1. the C language uses the getopt_long function. The Code is as follows: /*************************************** **************************************** \ File * main. c * \ Brief ** \ Author * Hank * \ Created date * 2013-03-12 ************************ **************************************** * **************/# include <stdio. h> # include <stdlib. h> # include <string. h> # include <getopt. h> extern char * optarg; extern int opterr; struct option opts [] = {"Ip", required_argument, NULL, 'I'}, {"port", required_argument, NULL, 'P'}, {"host", required_argument, NULL, 'S '}, {"out", required_argument, NULL, 'O'}, {"help", required_argument, NULL, 'H'}, {0, 0, 0, 0 }}; int parse_params (int argc, char ** argv, char * ip, int * port, char * host, char * f); int main (int argc, char * argv []) {char ip [32] = "225.1.1.31"; int port = 1234; char host [32] = "127.0. 0.1 "; char filename [512] =" udp. dat ";/* Parsing command-line parameters */parse_params (argc, argv, ip, & port, host, filename); return 0;} int parse_params (int argc, char ** argv, char * ip, int * port, char * host, char * f) {int c, index; opterr = 0; while (c = getopt_long (argc, argv, "I: p: s: o: h", opts, NULL ))! =-1) {switch (c) {case 'I': strcpy (ip, optarg); break; case 'p': * port = atoi (optarg); break; case's ': strcpy (host, optarg); break; case 'O': strcpy (f, optarg); break; case 'H': default: printf ("Usage: \ n "); printf ("-I ip: set udp's ip address \ n "); printf ("-p port: set udp's port \ n "); printf ("-s host: set local addresss \ n"); printf ("-o file: set output filename \ n"); printf ("-h: print help information \ n "); return 1 ;}/ * show banner */printf (" ip: % s \ nport: % d \ nhost: % s \ nfile: % s \ n ", ip, * port, host, f); for (index = optind; index <argc; index ++) printf ("Non-option argument % s \ n", argv [index]); return 0;} 2. for Perl, use the Getopt: Long module: http://search.cpan.org/~jv/Getopt-Long-2.39/lib/Getopt/Long.pm The Code is as follows :#! /Usr/bin/perl ################################## ######################################## #####\ File # parseing_args.pl # \ Brief ##\ Author # Hank # \ Created date #2013-03-14 ################ ######################################## ###################### use Getopt:: Long; my ($ params, $ key, $ verbose, $ help); my $ argc =$ # ARGV; # Number of input parameters my $ result = GetOptions ("params | p = s" => \ $ params, "key | k = I" => \ $ key, "ve Rbose "=>\$ verbose," help "=>\$ help); if ($ help = 1 | $ argc =-1) {print" Usage: \ n "; print ". /parsing_args.pl -- params = \"... \ "-- key = 1234 -- verbose \ n"; exit-1;} print "PARAMS:", $ params, "\ n"; print "MSG_KEY:", $ key, "\ n"; print "VERBOSE:", $ verbose, "\ n"; parameters are case-insensitive, and -- H -- h is acceptable. Some parameter descriptions :!: The option does not take an argument and may be negated by prefixing it with "no" or "no-". E. g. "foo! "Will allow -- foo (a value of 1 will be assigned) as well as -- nofoo and -- no-foo (a value of 0 will be assigned ). if the option has aliases, this applies to the aliases as well. using negation on a single letter option when bundling is in effect is pointless and will result in a warning. +: The option does not take an argument and will be incremented by 1 every time it appears on the command line. e. g. "more +", when used with -- more, will increment the value three times, resulting in a value of 3 (provided it was 0 or undefined at first ). the + specifier is ignored if the option destination is not a scalar. = type [desttype] [repeat] The option requires an argument of the given type. supported types are: s: String. an arbitrary sequence of characters. it is valid for the argument to start with-or --. i: Integer. an optional leading plus or minus sign, followed by a sequence of digits. o: Extended integer, Perl style. this can be either an optional leading plus or minus sign, followed by a sequence of digits, or an octal string (a zero, optionally followed by '0', '1 ',.. '7'), or a hexadecimal string (0x followed by '0 '.. '9', 'A '.. 'F', case insensitive), or a binary string (0b followed by a series of '0' and '1 '). f: Real number. for example 3.14,-6.23E24 and so on. the desttype can be @ or % to specify that the option is list or a hash valued. this is only needed when the destination for the option value is not otherwise specified. it shoshould be omitted when not needed. the repeat specifies the number of values this option takes per occurrence on the command line. it has the format: {[min] [, [max]}. min denotes the minimal number of arguments. it defaults to 1 for options with = and to 0 for options with:, see below. note that min overrules the =/: semantics. max denotes the maximum number of arguments. it must be at least min. if max is omitted, but the comma is not, there is no upper bound to the number of argument values taken.: type [desttype] Like =, but designates the argument as optional. if omitted, an empty string will be assigned to string values options, and the value zero to numeric options. note that if a string argument starts with-or --, it will be considered an option on itself.: number [desttype] Like: I, but if the value is omitted, the number will be assigned.: + [desttype] Like: I, but if the value is omitted, the current value for the option will be incremented.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.