Sponsored Content
Top Forums Programming Reading command line arguments and setting up values if option not provided Post 302611423 by kristinu on Thursday 22nd of March 2012 08:19:56 PM
Old 03-22-2012
Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value.

I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very large with lot of options, so need a scheme that can facilitate the development and maintenance of the code .

Code:
   Parsing  Pc(argc, argv);           // Pass all arguments
 
  const double  DefdAng = 2.0;
   double  dAng = DefdAng;
   if (Pc.GetDouble("DANG", dAng)) {   // User supplied value for dAng using DANG option. 
       cout << "User supplied dAng value = " << dAng << endl;
  }

Code:
  Parsing  Pc(argc, argv);           // Pass all arguments

  const double  DefdAng = 2.0;
  double  dAng;
  if (Pc.GetDouble("DANG", dAng)) {   // User supplied value for dAng using DANG option. 
      cout << "User supplied dAng value = " << dAng << endl;
  else {
      dAng = DefdAng;
       cout << "dAng set to default value of " << dAng << endl;
   }

Code:
   Parsing  Pc(argc, argv);           // Pass all arguments
 
  const double  DefdAng = 2.0;
   double  dAng;
   if (!Pc.GetDouble("DANG", dAng)) {
       dAng = DefdAng;
         cout << "dAng set to default value of " << dAng << endl;
     else {      // User supplied value for dAng using DANG option. 
      cout << "User supplied dAng value = " << dAng << endl;
    }

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Reading 2 arguments from a command line

If no arguments are entered I wanna be able to read 2 arguments, i have done like this but it doesnt work: x=0 until #loop starts do if ; then echo No arguments were entered, please enter 2 arguments. read $1 $2 elif || ; then echo $#... (0 Replies)
Discussion started by: Vozx
0 Replies

2. UNIX and Linux Applications

Reading values from the command line

Hi I want to give the user the choice of whether or not they want to include a certain option when they run the script. This is my getops: while getopts " s: d: r f: e h " option do case $option in f ) dsxfile="$OPTARG";; d ) dbname="$OPTARG";; s ) dsn="$OPTARG";; r )... (0 Replies)
Discussion started by: ladyAnne
0 Replies

3. Homework & Coursework Questions

trouble understanding file option and command line arguments

Hi, I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
Discussion started by: heywoodfloyd
1 Replies

4. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

5. Shell Programming and Scripting

Command line arguments with multiple values

how can I pass multiple values from command line arguments example script.sh -arg1 op1 -arg2 op1 op2 op3 (2 Replies)
Discussion started by: nsk
2 Replies

6. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

7. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

8. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

9. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

10. Shell Programming and Scripting

Reading properties from file and setting variable values

I want to read properties from a file and print evaluated values of each key. I am using AIX6.1. myfile.props protocol=http siteA.host=siteAhostname pageA=pageNameA siteAURL1=${protocol}:/${siteA.host}/pagea/blabla?v1=32 siteAURL2=${protocol}:/${siteA.host}/${pageA}/blabla?v1=32... (5 Replies)
Discussion started by: kchinnam
5 Replies
IPSEC_OPTIONSFROM(3)						    16 Oct 1998 					      IPSEC_OPTIONSFROM(3)

NAME
ipsec_optionsfrom - read additional ``command-line'' options from file SYNOPSIS
#include <freeswan.h> const char *optionsfrom(char * filename, int * argcp, char *** argvp, int optind, FILE * errsto); DESCRIPTION
Optionsfrom is called from within a getopt_long(3) scan, as the result of the appearance of an option (preferably --optionsfrom) to insert additional "command-line" arguments into the scan immediately after the option. Typically this would be done to pick up options which are security-sensitive and should not be visible to ps(1) and similar commands, and hence cannot be supplied as part of the actual command line or the environment. Optionsfrom reads the additional arguments from the specified filename, allocates a new argument vector to hold pointers to the existing arguments plus the new ones, and amends argc and argv (via the pointers argcp and argvp, which must point to the argc and argv being supplied to getopt_long(3)) accordingly. Optind must be the index, in the original argument vector, of the next argument. If errsto is NULL, optionsfrom returns NULL for success and a pointer to a string-literal error message for failure; see DIAGNOSTICS. If errsto is non-NULL and an error occurs, optionsfrom prints a suitable complaint onto the errsto descriptor and invokes exit with an exit status of 2; this is a convenience for cases where more sophisticated responses are not required. The text of existing arguments is not disturbed by optionsfrom, so pointers to them and into them remain valid. The file of additional arguments is an ASCII text file. Lines consisting solely of white space, and lines beginning with #, are comments and are ignored. Otherwise, a line which does not begin with - is taken to be a single argument; if it both begins and ends with double-quote ("), those quotes are stripped off (note, no other processing is done within the line!). A line beginning with - is considered to contain multiple arguments separated by white space. Because optionsfrom reads its entire file before the getopt_long(3) scan is resumed, an optionsfrom file can contain another --optionsfrom option. Obviously, infinite loops are possible here. If errsto is non-NULL, optionsfrom considers it an error to be called more than 100 times. If errsto is NULL, loop detection is up to the caller (and the internal loop counter is zeroed out). EXAMPLE
A reasonable way to invoke optionsfrom would be like so: #include <getopt.h> struct option opts[] = { /* ... */ "optionsfrom", 1, NULL, '+', /* ... */ }; int main(argc, argv) int argc; char *argv[]; { int opt; extern char *optarg; extern int optind; while ((opt = getopt_long(argc, argv, "", opts, NULL)) != EOF) switch (opt) { /* ... */ case '+': /* optionsfrom */ optionsfrom(optarg, &argc, &argv, optind, stderr); /* does not return on error */ break; /* ... */ } /* ... */ SEE ALSO
getopt_long(3) DIAGNOSTICS
Errors in optionsfrom are: unable to open file; attempt to allocate temporary storage for argument or argument vector failed; read error in file; line too long. HISTORY
Written for the FreeS/WAN project by Henry Spencer. BUGS
The double-quote convention is rather simplistic. Line length is currently limited to 1023 bytes, and there is no continuation convention. The restriction of error reports to literal strings (so that callers don't need to worry about freeing them or copying them) does limit the precision of error reporting. The error-reporting convention lends itself to slightly obscure code, because many readers will not think of NULL as signifying success. There is a certain element of unwarranted chumminess with the insides of getopt_long(3) here. No non-public interfaces are actually used, but optionsfrom does rely on getopt_long(3) being well-behaved in certain ways that are not actually promised by the specs. 16 Oct 1998 11/14/2008 IPSEC_OPTIONSFROM(3)
All times are GMT -4. The time now is 07:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy