Unix/Linux Go Back    

Plan 9 - man page for arg (plan9 section 2)

Linux & Unix Commands - Search Man Pages
Man Page or Keyword Search:   man
Select Man Page Set:       apropos Keyword Search (sections above)

ARG(2)											   ARG(2)

       ARGBEGIN, ARGEND, ARGC, ARGF, arginit, argopt - process option letters from argv

       #include <u.h>
       #include <libc.h>

       ARGBEGIN {
       char *ARGF();
       Rune ARGC();
       } ARGEND

       extern char *argv0;

       /* Alef only */

       Arg  *arginit(int argc, byte **argv);

       Rune argopt(Arg *arg);

       byte *argf(Arg *arg);

       These  macros  assume  the  names  argc	and argv are in scope; see exec(2).  ARGBEGIN and
       ARGEND surround code for processing program options.  The code should be the cases of a	C
       switch  on  option characters; it is executed once for each option character.  Options end
       after an argument --, before an argument -, or before an argument that doesn't begin  with

       ARGC() returns the current option character.

       ARGF()  returns the current option argument: a pointer to the rest of the option string if
       not empty, or the next argument in argv if any, or 0.  ARGF must be called just	once  for
       each option that takes an argument.

       After ARGBEGIN, argv0 is a copy of argv[0] (conventionally the name of the program).

       After ARGEND, argv points at a zero-terminated list of the remaining argc arguments.

       The  Alef argument processing routines are unrelated.  Instead, an aggr called Arg is ini-
       tialized by a call to arginit.  Successive calls to argopt return successive option  char-
       acters,	or  zero at the end of the options.  After a call to argopt, argf will return any
       argument string associated with the option.

       This C program can take option b and option f, which requires an argument.

	      #include <u.h>
	      #include <libc.h>
	      main(int argc, char *argv[])
		      char *f;
		      print("%s", argv[0]);
		      ARGBEGIN {
		      case 'b':
			      print(" -b");
		      case 'f':
			      print(" -f(%s)", (f=ARGF())? f: "no arg");
			      print(" badflag('%c')", ARGC());
		      } ARGEND
		      print(" %d args:", argc);
			      print(" '%s'", *argv++);

       Here is the output for the run prog -bffile1 -r -f file2 arg1 arg2

	      prog -b -f(file1) badflag('r') -f(file2) 2 args: 'arg1' 'arg2'

       This Alef program accepts options b and, with an attached file name, f.

	      #include <alef.h>
	      main(int argc, byte **argv)
		      int a, ac, bflag;
		      byte *file;
		      Arg *arg;

		      arg = arginit(argc, argv);
		      while(ac = argopt(arg)) switch(ac){
		      case 'b':
			      bflag = 1;
		      case 'f':
			      file = argf(arg);
		      for(a=0; a<arg->ac; a++)
			      print("argument %s\n", arg->av[a]);


Unix & Linux Commands & Man Pages : ©2000 - 2018 Unix and Linux Forums

All times are GMT -4. The time now is 07:31 PM.