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 .
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT DEBIAN
ipsec_optionsfrom
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)