Changing the way arguments are read from program


 
Thread Tools Search this Thread
Top Forums Programming Changing the way arguments are read from program
# 1  
Old 02-16-2012
Changing the way arguments are read from program

I have the following piece of code. Currently the command line arguments are passed as shown below using the "= "sign. I capture the name of the argument, for example vmod and it's corresponding user parameter which is jcdint-z30.cmd.

Code:
./raytrac vmod=jcdint-z30.cmd srFile=jcdint.sr

Now I want to be able to calling raytrac using

Code:
./raytrac --vmod jcdint-z30.cmd --srFile jcdint.sr

Code:
void  Parsing::ParseCmd(
  const int  argc,
  char*  argv[]) {

  int  i;
  int  poseq;

  String  S;
  String  Name;
  String  File;
  String  Ends;

  List<int>  Ord; Ord += 0;

  ifstream  ifs;

  for (i = 1; i < argc; i++) {

      S = String(argv[i]);
      if (S[0] == '?') {
          File = S.ToEnd(1);
          ifs.open((char *)File);
          if (ifs.bad()) { error("Error including file"); }
          ParseFile(ifs);
          ifs.close();
      } else {
          Search(S, "=", poseq);
          Name = ToUpper( S.Substr(0, poseq-1) );
          Ends = S.ToEnd( poseq + 1 );
          Index += ParseEl(Name, Ends, Ord);
      }

  }

  Ptr = 0;

}

# 2  
Old 02-16-2012
There's nothing in there which would prevent that. The problem's in the rest of the program I presume.
# 3  
Old 02-16-2012
I have changed the code as follows. Any suggestions would be appreciated.
I want to check if the -- is in the front though so then I can take the name. How can I do this?

Code:
void  Parsing::ParseCmd(
  const int  argc,
  char*  argv[]) {

  int  i;
  int  k;
  int  poseq;
  int  skipFlag;

  String  S;
  String  Name;
  String  File;
  String  Ends;

  ifstream  ifs;

  List<int>  Ord;
  Ord += 0;

  skipFlag = 0;
  for (i = 1; i < argc; i++) {

      if (skipFlag == 1) {
        skipFlag = 0;
        continue;                                     // Skips the iteration
      }

      S = String(argv[i]);
      if (S[0] == '?') {
          File = S.ToEnd(1);
          ifs.open((char *)File);
          if (ifs.bad()) { error("Error including file"); }
          ParseFile(ifs);
          ifs.close();
      } else {
          Search(S, "--", poseq);
          Name = ToUpper( S.ToEnd( poseq + 2 ) );
          Ends = String(argv[i+1]);
          Index += ParseEl(Name, Ends, Ord);
          skipFlag = 1;
          cout << Name << " "<< Ends << " " << poseq << "\n";
      }

  }

  Ptr = 0;

}

# 4  
Old 02-16-2012
Sorry for misunderstanding the original question.

Code:
#include <string.h>

...

if(strncmp("--", argv[i], 2)==0)
{
...
}

# 5  
Old 02-16-2012
Ok, so I now have the following two functions and want to integrate them together so that the user can pass arguments in two different ways:

Code:
--vmod=jcdint.cmd

or

Code:
--vmod  jcdint.cmd

Code:
void  Parsing::ParseCmd(
  const int  argc,
  char*  argv[]) {

  int  i;
  int  poseq;

  String  S;
  String  Name;
  String  File;
  String  Ends;

  List<int>  Ord; Ord += 0;

  ifstream  ifs;

  for (i = 1; i < argc; i++) {

      S = String(argv[i]);
      if (S[0] == '?') {
          File = S.ToEnd(1);
          ifs.open((char *)File);
          if (ifs.bad()) { error("Error including file"); }
          ParseFile(ifs);
          ifs.close();
      } else {
          Search(S, "=", poseq);
          Name = ToUpper( S.Substr(0, poseq-1) );
          Ends = S.ToEnd( poseq + 1 );
          Index += ParseEl(Name, Ends, Ord);
      }

  }

  Ptr = 0;

}

////////////////////////////////////////////////////////////////////////////////////////////////////

// Reading arguments from the command line

void  Parsing::ParseCmdLine(
  const int  argc,
  char*  argv[]) {

  int  i;
  int  k;
  int  poseq;
  int  skipFlag;
  int  StrSize;

  String  S;
  String  Name;
  String  File;
  String  Ends;
  String  ErrMsg;

  ifstream  ifs;

  List<int>  Ord;
  Ord += 0;

  skipFlag = 0;
  for (i = 1; i < argc; i++) {

      if (skipFlag == 1) {
        skipFlag = 0;
        continue;                                     // Skips the iteration
      }

      S = String(argv[i]);
      StrSize = S.size();
      if (S[0] == '?') {
          File = S.ToEnd(1);
          ifs.open((char *)File);
          if (ifs.bad()) { error("Error including file"); }
          ParseFile(ifs);
          ifs.close();
      } else {
          Search(S, "--", poseq);
          cout << poseq << "\n";
          if (poseq == 0) { Name = ToUpper( S.ToEnd( poseq + 2 ) );
          } else { error("Bad command line argument: " + S);
          }

          Ends = String(argv[i+1]);
          Index += ParseEl(Name, Ends, Ord);
          skipFlag = 1;
          cout << Name << " "<< Ends << " " << poseq << "\n";
      }

  }

  Ptr = 0;

}

# 6  
Old 02-17-2012
I've had a hard time helping you because haven't documented your code at all, you use all kinds of things you never declared anywhere, and use lots of functions which aren't standard STL. I have to guess at what the meaning of all of these is. I figured out your ToEnd function eventually, but many things remain mysterious. To that end, I'm forced to rewrite it instead of modifying it.

You can simplify this a lot, anyway. There's no reason to keep converting most of those things to String and back when all you do is convert them back to 'char *'. [] works just as well on char *'s as it does on strings too, and far faster.

You can avoid SkipFlag. Increment the loop right now if you need to skip something, and you won't need to remember later.

Most of your variables make much more sense to declare where you're using them, instead of in one big lump at the top of your function. It took me a while to realize that each code block uses separate, independent variables here, even though they all just use the value immediately...

You can avoid the close() by declaring ifs inside the code block, too. When you do that, it auto-closes whenever it goes out of scope. This will also let you get rid of the open(), because you can feed the file name straight into the constructor.

Code:
#include <string.h>

...

void Parsing::ParseCMD(int argc, char *argv[])
{
        List<int>  Ord; Ord += 0;
        int n;

        for(n=1; n<argc; n++)
        {
                if(argv[n][0] == '?') // "?abcd"[0] is '?'.
                {
                        ifstream ifs(argv[n]+1); // "?abcd"+1 is "abcd", so open filename "abcd".
                        if(!ifs.good()) error("Error including file");
                        else    ParseFile(ifs);
                } // end of code block.  ifs closes here.
                else if(strncmp(argv[n], "--", 2) == 0) // compare first 2 chars.  Will be 0 if they match.
                {
                        // strchr("--asdf=b", '=') is "=b".  If not found, it is NULL instead.
                        const char *After=strchr(argv[n], '='), *Name;
                        string Ends; // Make it a 'string' so we can use your ToUpper function

                        if(After == NULL)
                        {
                                n++; // Get the next argument
                                // If there is no next argument, quit instead of crashing!
                                if(n == argc)
                                {
                                        error("out of arguments");
                                        continue;
                                }

                                Name=argv[n];
                        }
                        else                    Name=After+1; // "=b"+1 is "b"

                        Ends=ToUpper(Name);

                        Index += ParseEl(Name, Ends, Ord);
                }
        }
}


Last edited by Corona688; 02-17-2012 at 12:00 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-17-2012
Thanks a lot. I did not include a lot of things as the program is quite large.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass command line arguments to awk program?

#!/bin/awk -f BEGIN { FS=":"; } { if ( $7 == "" ) { print $1 ": no password!"; } } I want to execute this program for a particular user to check for his password from the file /etc/passwd (as the input file) and the user details to be given... (1 Reply)
Discussion started by: sri.phani
1 Replies

2. Shell Programming and Scripting

Multiple arguments to read

I am developing a script where 3 other scripts are included. This is a graph related script. COMPLETE IDEA: -There are 3 different graph scripts. I would like to create a master graph with all 3 in one. -User chooses the type of graph -User is asked to enter the required auguments (... (7 Replies)
Discussion started by: newkid.7955
7 Replies

3. Shell Programming and Scripting

Interpreting arguments in Perl program.

Hello. I'm new to Perl and I am not sure how to interpret command line arguments in the program. I am writing a program similar to the Unix utility 'tail' and need to check if first argument is '-1' (1) or any arbitrary number of lines to output. How would I write an 'if' statement to check for... (4 Replies)
Discussion started by: D2K
4 Replies

4. Programming

Writing C++ program Arguments and Classes

I want to write a C++ program that uses a class to do some calculations. I pass arguments to the program, some of which are used to set up class members. A class function will then perform the necessary calculations. I am wondering how I should pass the arguments from the program to set the... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Programming

Program java arguments

Hello, The arguments are strings. In my code I need them to be a different type, I do the cast but it is not feasible ... Have you any idea? Thank you (8 Replies)
Discussion started by: chercheur857
8 Replies

6. Shell Programming and Scripting

How to give runtime arguments to different program

I have a shell script that is attempting to call a c program. I call the c program with ./dictool dictool accepts arguments at runtime. It works by prompting the user for various commands, acting on those commands, spitting out an output, and then prompting for more commands. My question is,... (1 Reply)
Discussion started by: aarongoldfein
1 Replies

7. Shell Programming and Scripting

pass arguments to called program

Thank you very much. (2 Replies)
Discussion started by: ShellUser
2 Replies

8. Linux

Where to set program arguments in DDD debugger?

In DDD debugger, where to set the the arguments for main program? For example: ./myExe "argv1" "argv2" -> where to set "argv1" & "argv2" ? Thanks! (2 Replies)
Discussion started by: princelinux
2 Replies

9. Shell Programming and Scripting

two arguments with nawk program

Can someone help me to understand this part of code? /bin/nawk -f awkfile file1 file2 I know awkfile is the one with awk script. file1 is source file that needs to be processed. What is file2 two? Thanks for your help! (4 Replies)
Discussion started by: whatisthis
4 Replies

10. UNIX for Advanced & Expert Users

Arguments to a shell program

Hi List, Is it possible to pass one argument to a shell program eg) there is a shell program abc which takes one arguments abc one Due to some reasons I pass abc one two Now one,two must be considered as "one" argument to the shell programs. Any suggestions,hints are welcome. ... (3 Replies)
Discussion started by: csvenkata
3 Replies
Login or Register to Ask a Question