Sponsored Content
Full Discussion: Input arguments with C++
Top Forums Programming Input arguments with C++ Post 302756755 by Corona688 on Wednesday 16th of January 2013 12:43:20 PM
Old 01-16-2013
Since these are supposed to be global, putting them into a class doesn't make much sense, how about a namespace instead?

Code:
namespace options {
        int x, y, z;

        bool parse(int argc, char *argv[])
        {
                x=5;
                y=6;
                z=7;
        }
};

int main(int argc, char *argv[])
{
        if(!options::parse(argc, argv))
        {
                return(1);
        }

        printf("Got x %d\n", options::x);
        printf("Got y %d\n", options::y);
        printf("Got z %d\n", options::z);
        return(0);
}

That's with it all in one file. If you wanted the namespace accessible from multiple different C++ files, it'd be like this:

Code:
#ifndef __NS_H__
#define __NS_H__

namespace options {
        extern int x, y, z;
        bool parse(int argc, char *argv[]);
};

#endif/*__NS_H__*/

Code:
#include <stdio.h>
#include "ns.h"

int main(int argc, char *argv[])
{
        if(!options::parse(argc, argv))
        {
                return(1);
        }

        printf("Got x %d\n", options::x);
        printf("Got y %d\n", options::y);
        printf("Got z %d\n", options::z);
        return(0);
}

// These definitions only belong in one single file
int options::x=-1, options::y=-1, options::z=-1;

bool options::parse(int argc, char *argv[])
{
        x=5;
        y=6;
        z=7;
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

2. Shell Programming and Scripting

Accepting user input and arguments in PERL

Hi All, Can we pass arguments while calling the perl script and as well as ask user input during execution of the script? My program is as below: I am passing arg1 and arg2 as argements to test.pl ]./test.pl arg1 arg2 Inside the test.pl I have : print "Do you want a name ? (y/n) : ";... (2 Replies)
Discussion started by: jisha
2 Replies

3. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

6. Shell Programming and Scripting

[Solved] Read and validate input arguments

Hi, I need to get input arguments, as well as validate them. This is how I'm reading them: #!/bin/bash args="$@" # save arguments to variable ## Read input arguments, if so while ; do case $1 in -v | --verbose ) verbose=true;; -z | --gzip ) compression="gz";; ... (3 Replies)
Discussion started by: AlbertGM
3 Replies

7. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

8. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

9. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

10. Shell Programming and Scripting

How to pass arguments based on input file?

This script is running some exe file we are passing three argumnet below custome key word Want to update script based on input files every time it will take argument from input file below is the input files should take this input put it into the script. k.ksh cd /u/kali/temp ... (8 Replies)
Discussion started by: Kalia
8 Replies
subgetopt(3)						     Library Functions Manual						      subgetopt(3)

NAME
subgetopt - get option character from command line SYNTAX
#include <subgetopt.h> char *sgoptarg; int sgoptind; int sgoptpos; int sgoptdone; int sgoptproblem; int sgopt(argc,argv,opts); int argc; char **argv; char *opts; DESCRIPTION
sgopt returns the next valid command-line option character from argv. Valid option characters are listed in the opts string. opts may be empty. A character in opts may be followed by a colon, in which case it takes an option argument. Avoid using the characters ?, :, and - as option characters. Below option argument is abbreviated as optarg and command-line argument is abbreviated as cmdarg. Options are listed in cmdargs which begin with a minus sign. Several options which do not take optargs may be combined into one cmdarg. An option which takes an optarg may be handled in two ways. If it appears at the very end of a cmdarg, then the entire next cmdarg is the optarg. But if there are any characters in the cmdarg after the option character, then those characters form the optarg. The optarg is returned in sgoptarg. Next time sgopt looks at the cmdarg which follows the optarg. If a cmdarg does not begin with a hyphen, or if it is a lone hyphen not followed by any characters, or if it begins with two hyphens, then it terminates option processing, and sgopt returns an appropriate code. If there are two hyphens, sgopt will advance attention to the next cmdarg, so it can be called again to read further options. PROPER USAGE
sgoptproblem should be used only when sgopt returns ?. sgoptind and sgoptpos are defined all the time. sgoptarg is defined all the time; it is null unless sgopt has just returned an option with optarg. sgopt is typically used as follows. #include <subgetopt.h> main(argc,argv) int argc; char **argv; { int opt; while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone) switch(opt) { case 'a': printf("opt a with optarg %s ",sgoptarg); break; case 's': printf("opt s with no optarg "); break; case '?': if (argv[sgoptind] && (sgoptind < argc)) printf("illegal opt %c ",sgoptproblem); else printf("missing arg, opt %c ",sgoptproblem); exit(1); } argv += sgoptind; while (*argv) printf("argument %s ",*argv++); exit(0); } The end of the command line is marked by either argc, or a null pointer in argv, whichever comes first. Normally these two markers coin- cide, so it is redundant to test for both argv[sgoptind] and sgoptind < argc. The above code shows both tests as an illustration. Multiple option sets: One useful technique is to call sgopt with a primary opts until it returns EOF, then call sgopt with a secondary opts until it returns EOF. The user can provide primary options, then a double hyphen, and then secondary options. No special handling is needed if some or all of the options are omitted. The same technique can be used for any number of option sets in series. Multiple command lines: Before parsing a new argv, make sure to set sgoptind and sgoptpos back to 1 and 0. PARSING STAGES
sgopt keeps track of its position in argv with sgoptind and sgoptpos, which are initialized to 1 and 0. It looks at argv[sgoptind][sgopt- pos] and following characters. sgopt indicates that no more options are available by returning sgoptdone, which is initialized to SUBGETOPTDONE, which is defined as -1. sgopt begins by setting optarg to null. Ending conditions: If argv is null, or sgoptind is larger than argc, or the current cmdarg argv[sgoptind] is null, then sgopt returns opt- done. Stage one: If the current character is zero, sgopt moves to the beginning of the next cmdarg. It then checks the ending conditions again. Stage two: If the current position is the begining of the cmdarg, sgopt checks whether the current character is a minus sign. If not it returns optdone. It then moves to the next character. If that character is zero, sgopt moves back to the beginning of the cmdarg, and returns sgoptdone. If the character is a minus sign, sgopt moves to the beginning of the next cmdarg, and returns sgoptdone. Stage three: sgopt records the current character, c, and moves to the next character. There are three possibilities: (1) c is an option character without optarg in opts, or (2) c is an option character with optarg in opts, or (3) c does not appear in opts. (1) If c appears as an option character without optarg in opts, sgopt returns c. (2) If c appears as an option character with optarg in opts, sgopt sets sgoptarg to the current position, and moves to the next cmdarg. If sgoptarg is nonempty, sgopt returns c. Then sgopt sets sgoptarg to the current cmdarg. If the current cmdarg is null, or past argc, sgopt sets sgoptproblem to c and returns ?. Otherwise sgopt moves to the next argument and returns c. (2) If c does not appear in opts, sgopt sets sgoptproblem to c and returns ?. SYNTAX NOTE
sgopt is actually a macro abbreviation for subgetopt. The external sg variables are also macros for subget. These macros are defined in <subgetopt.h>, unless SUBGETOPTNOSHORT is defined when <subgetopt.h> is included. VERSION
subgetopt version 0.9, 931129. AUTHOR
Placed into the public domain by Daniel J. Bernstein. subgetopt(3)
All times are GMT -4. The time now is 10:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy