Changing the way arguments are read from program


 
Thread Tools Search this Thread
Top Forums Programming Changing the way arguments are read from program
# 8  
Old 02-17-2012
I understand, but you chopped out a bit too much, and forgot to explain any of your logic...
# 9  
Old 03-07-2012
I am getting back to this but I am confused about something. The problem concerns the variable Name.

Code:
String Ends = ToUpper(Name);

As ToUpper uses my defines type called String.
# 10  
Old 03-07-2012
That's the only thing I used your string type for, yes -- converting to uppercase.

Everything else is just (original pointer + characters offset), but that actually needed to change the string, so I used your String. I could've written my own version of a toupper, but you'd have to worry about freeing it later, so it was simpler to use yours.

What was the question?
# 11  
Old 03-07-2012
The routine I have got now is this one. However, it does not seem to work. Something is going wrong but cannot figure it out yet.

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

  int  i;
  int  StrSize;                        // Size of string
  int  EndsSize;                       // Size of string

  List<int>  Ord;
  Ord += 0;

  cout << "ParseCmdLine2 argv[i]" << endl;
  cout << " i = " << i << ", argv[i] = " << argv[i] << endl;
  cout << "argc = " << argc << endl;

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

//      S = String(argv[i]);
//      StrSize = S.size();
      cout << "i = " << i << endl;

      // First character is '?', e.g. "?abcd"[0] is '?'.
      if (argv[i][0] == '?') {
      cout << "Check ?" << endl;

          ifstream ifs(argv[i]+1);     // "?abcd"+1 is "abcd", so open filename "abcd".
          if ( ifs.bad() ) error("Error including file " + String(argv[i]) );
          else ParseFile(ifs);

      }                                // end of code block. ifs closes here.

      // First 2 chars are '-'. A zero value indicates that the characters compared in both
      // strings are all equal.
      else if (strncmp(argv[i], "--", 2) == 0)
      {

      cout << "Check -- long option" << endl;
          // Pointer to first occurrence of '='
          // strchr("--asdf=b", '=') is "=b". If '=' is not found, function returns null pointer.
          const char* After = strchr(argv[i], '=');

          const char* Name;
//          String Ends;                 // Make it a 'String' so we can use ToUpper.

          // '=' is not found. Get user input from next argument.
          if (After == NULL)
          {
            i++;                       // ++i is more efficient than i++.
            if (i == argc) {           // If next argument not found, quit instead of crashing.
              error("Out of arguments");
              continue;
            }
            Name = argv[i];            // Set user input

          // '=' is found.
          } else {
            Name = After + 1;          // Set user input. "=file"+1 is "file".
          }
          String Ends = ToUpper(String(Name)); // Conversion of 'Name' to a 'String' so we can use ToUpper.
          cout << "Name = " << Name << ", Ends = " << Ends << endl;
          Index += ParseEl(Name, Ends, Ord);
      }

  }

  Ptr = 0;

}

---------- Post updated at 06:19 PM ---------- Previous update was at 06:13 PM ----------

I think I am making some progress. Name should have the string to the left of '=' whereas Ends should have the string to the right of '='.

The following is the original code:

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;

}

---------- Post updated at 06:34 PM ---------- Previous update was at 06:19 PM ----------

We are not capturing the Name.

If the user parses vmod=jcdint-z30.cmd, I want to have Name=VMOD and Ends=jcdint-z30.cmd

---------- Post updated at 07:02 PM ---------- Previous update was at 06:34 PM ----------

Some more progress. I now have the code

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

  int  i;
  int  StrSize;                        // Size of string
  int  EndsSize;                       // Size of string

  List<int>  Ord;
  Ord += 0;

  cout << "ParseCmdLine2 argv[i]" << endl;
  cout << " i = " << i << ", argv[i] = " << argv[i] << endl;
  cout << "argc = " << argc << endl;

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

//      S = String(argv[i]);
//      StrSize = S.size();
      cout << "i = " << i << endl;
      cout << "argv[i] = " << argv[i] << endl;
      cout << "strncmp = " << strncmp(argv[i], "--", 2) << endl;

      // First character is '?', e.g. "?abcd"[0] is '?'.
      if (argv[i][0] == '?') {
      cout << "Check ?" << endl;

          ifstream ifs(argv[i]+1);     // "?abcd"+1 is "abcd", so open filename "abcd".
          if ( ifs.bad() ) error("Error including file " + String(argv[i]) );
          else ParseFile(ifs);

      }                                // end of code block. ifs closes here.

      // First 2 chars are '-'. A zero value indicates that the characters compared in both
      // strings are all equal.
      else if (strncmp(argv[i], "--", 2) == 0)
      {

          cout << "Check long option" << endl;
          // Pointer to first occurrence of '='
          // strchr("--asdf=b", '=') is "=b". If '=' is not found, function returns null pointer.
          const char* After = strchr(argv[i], '=');
          const char* optName;
          const char* Ends;
          String Name;                 // Make it a 'String' so we can use ToUpper.

          // '=' is not found. Get user input from next argument.
          if (After == NULL)
          {
            i++;                       // ++i is more efficient than i++.
            if (i == argc) {           // If next argument not found, quit instead of crashing.
              error("Out of arguments");
              continue;
            }
            Ends = argv[i];            // Set user input

          // '=' is found.
          } else {
            Ends = After + 1;          // Set user input. "=file"+1 is "file".
          }
            Name = "VMOD";
//          String optName = ToUpper(); // Conversion of 'Name' to a 'String' so we can use ToUpper.
          cout << "Name = " << Name << ", Ends = " << Ends << endl << endl;
//          Index += ParseEl(Name, Ends, Ord);
      }

  }

  Ptr = 0;

}


After passing

Code:
/home/chrisd/tatsh/trunk/hstmy/bin/prog/raytrac --vmod=jcdint-z30.cmd --srfile=jcdint.sr --rcfile=jcdint.rc --phases=P --level=twopt --format="X T" --dtau=0.1 mdacc=0.3 --mindist=0.03 --maxitertp=25 --ray=jcdint-z30.ry --out=jcdint-z30.xt --vrb=medium | & tee jcdint-z30-raytrac.log

I get the following out put

Code:
ParseCmdLine2 argv[i]
 i = 0, argv[i] = /home/chrisd/tatsh/trunk/hstmy/bin/prog/raytrac
argc = 14
i = 1
argv[i] = --vmod=jcdint-z30.cmd
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint-z30.cmd

i = 2
argv[i] = --srfile=jcdint.sr
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint.sr

i = 3
argv[i] = --rcfile=jcdint.rc
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint.rc

i = 4
argv[i] = --phases=P
strncmp = 0
Check long option
Name = VMOD, Ends = P

i = 5
argv[i] = --level=twopt
strncmp = 0
Check long option
Name = VMOD, Ends = twopt

i = 6
argv[i] = --format=X T
strncmp = 0
Check long option
Name = VMOD, Ends = X T

i = 7
argv[i] = --dtau=0.1
strncmp = 0
Check long option
Name = VMOD, Ends = 0.1

i = 8
argv[i] = mdacc=0.3
strncmp = 1
i = 9
argv[i] = --mindist=0.03
strncmp = 0
Check long option
Name = VMOD, Ends = 0.03

i = 10
argv[i] = --maxitertp=25
strncmp = 0
Check long option
Name = VMOD, Ends = 25

i = 11
argv[i] = --ray=jcdint-z30.ry
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint-z30.ry

i = 12
argv[i] = --out=jcdint-z30.xt
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint-z30.xt

i = 13
argv[i] = --vrb=medium
strncmp = 0
Check long option
Name = VMOD, Ends = medium


What I need to do is get the Name to be VMOD, SRFILE, RCFILE, ...
# 12  
Old 03-08-2012
Hello, I am new to the forum.
# 13  
Old 03-08-2012
I don't know where you got the idea i++ is more efficient than ++i. The operation is identical -- the only difference is the order.

I have absolutely no way to test this code because of the heavy use of variables and classes you never defined anywhere. Therefore, without you telling me in what way it's going wrong, it's extremely hard to tell what it is doing.

How about this:

Code:
#include <ctype.h> // for standard C toupper() function.  toupper('a') returns 'A'

// Reads 'var' part from 'var=whatever' in input, into VAR in 'buf'
void setname(char *buf, const char *input)
{
        char *e;
        strcpy(buf, input);
        e=strchr(buf, "=");
        if(e) (*e)='\0';

        e=buf;

        while(*e) { // Convert to uppercase, char by char
                (*e)=toupper(*e);
                e++;
        }   
}

...

void  Parsing::ParseCmdLine2(
  const int  argc,
  char*  argv[]) {
...
        char Namebuf[512];
...

        setname(Namebuf, argv[i]); // string class no longer needed!
...


Last edited by Corona688; 03-08-2012 at 04:18 PM..
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