Parse command-line arguments: Difference between revisions

(→‎{{header|C}}: add task)
Line 9:
<pre>200</pre>
 
=={{header|C}}==
The man page for getopt (man 3 getopt) provides better option handling with examples. But if you just want to parse one argument... (adapted from simple database task):
<lang c>#include <stdio.h>
int main(int argc, char **argv){
int i;
char *commands[]={"-c", "-p", "-t", "-d", "-a", NULL};
enum {CREATE,PRINT,TITLE,DATE,AUTH};
if (argc<2) {
usage: printf ("Usage: %s [commands]\n"
"-c Create new entry.\n"
"-p Print the latest entry.\n"
"-t Sort by title.\n"
"-d Sort by date.\n"
"-a Sort by author.\n",argv[0]);
return 0;
}
for (i=0;commands[i]&&strcmp(argv[1],commands[i]);i++);
switch (i) {
case CREATE:
...
break;
case PRINT:
...
break;
...
...
default:
printf ("Unknown command..." ...);
goto usage;
}
return 0;
}</lang>
=={{header|Go}}==
Most simply, implementing the suggested example from the talk page:
Line 44 ⟶ 76:
n: 99
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
The Icon Programming Library provides a procedure for processing command line options. See the library reference for detailed documentation. The code below is an example.
Anonymous user