Return-Path: william@bourbon.usc.edu Delivery-Date: Thu Sep 18 08:33:49 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m8IFXnsI027448 for ; Thu, 18 Sep 2008 08:33:49 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m8IFZF3V017969 for ; Thu, 18 Sep 2008 08:35:15 -0700 Message-Id: <200809181535.m8IFZF3V017969@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: Parsing commandline arguments... Date: Thu, 18 Sep 2008 08:35:15 -0700 From: Bill Cheng Someone wrote: > Is there a particular header file to be included for "Usage" ? I am getting > it as not defined on compiling. As I mentioned before, my code was incomplete. You also need to write Usage() or replace with any error reporting function you'd prefer. -- Bill Cheng // bill.cheng@usc.edu On Wed, Sep 17, 2008 at 2:07 PM, Bill Cheng wrote: > Hi everyone, > > If you are having trouble parsing commandline arguments, > especially with the optional ones, here is some sample code > on how to deal with them. > > Let's take the encrypt command as an example: > > mm2 -lambda 0.25 -mu 0.5 -n 8 -d exp > > Argc will be 9 and argv will be: > > argv[0] = "/...something.../mm2" > argv[1] = "-lambda" > argv[2] = "0.25" > argv[3] = "-mu" > argv[4] = "0.5" > argv[7] = "-n" > argv[8] = "8" > argv[9] = "-d" > argv[10] = "exp" > > Let's say that you pass argc and argv to ProcessOptions(). > Here's what ProcessOptions() can look like: > > int ProcessOptiona(int argc, char *argv[]) > { > /* initialize variables according to the spec */ > lambda = (double)0.5; > mu = (double)0.5; > n = 20; > distr = DISTR_EXP; > > /* initially, *argv is argv[0] */ > for (argc--, argv++; argc > 0; argc--, argv++) { > /* a commandline argument can either begin with '-' or not */ > if (*argv == '-') { > /* a commandline argument that begins with '-' */ > if (strcmp(*argv, "-lambda") == 0) { > argc--; argv++; > if (argc == 0) { > Usage("Lambda value missing."); > return (-1); > } > lambda = strtod(*argv, NULL); > } else if (strcmp(*argv, "-mu") == 0) { > argc--; argv++; > if (argc == 0) { > Usage("Mu value missing."); > return (-1); > } > mu = strtod(*argv, NULL); > } else if (strcmp(*argv, "-n") == 0) { > argc--; argv++; > if (argc == 0) { > Usage("N value missing."); > return (-1); > } > n = atoi(*argv, NULL); > } else if (strcmp(*argv, "-d") == 0) { > argc--; argv++; > if (argc == 0) { > Usage("Distribution specification missing."); > return (-1); > } > if (strcmp(*argv, "exp") == 0) { > distr = DISTR_EXP; > } else if (strcmp(*argv, "det") == 0) { > distr = DISTR_DET; > } else { > fprintf(stderr, "Invalid distribution: %s\n", > *argv); > Usage(NULL); > return (-1); > } > } else { > fprintf(stderr, "Invalid commandline option: %s\n", > *argv); > Usage(""); > return (-1); > } > } else { > Usage("Malformed command."); > return (-1); > } > } > return 0; /* 0 means okay */ > } > > This code is not complete since it's not doing error checking. > But you should get the idea of how to do it. Please try to > understand the above code and *adapt* it for your use for all > the assignments! > > If you have questions, please feel free to ask me! > -- > Bill Cheng // bill.cheng@usc.edu