How to call functions from the command line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AmmarN
    New Member
    • Mar 2007
    • 18

    How to call functions from the command line

    Hi all,

    I have a c++ programme that intakes a binary file and performs various operations on it. I am not gonna get in to the details of the operations, currently i specify the order in which the operations take place by calling different functions the way i write the main function. My question is -- Is there a way that a user can call different functions from this application from the command line.
    For example.

    say i have 3 functions e.g : Open,F1 , F2, F3.

    Now i would like to call the functions while the programme is running and i understand that i wont be able to do anything else at the command line while the prograame is running. Its an application so i wanna run the programme first and then call different functions while the programme is running such as:

    a.out Open (filename)
    a.out CallF1 (x,y,z)
    (after F1 produces its result e.g "Woohoohoo" then
    a.out CallF2 (a,b,c)
    .
    .
    .
    and Finally

    a.out Quit.

    meaning call different functions from the command line with the parameters.
    Any help with the structure of the main function to acheive the above will be highly appreciated.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    I assume when you mention command line you are talking about entering data at the keyboard while the program is executing (rather than the comand line when you start the program)??
    at the simplest level you could read a number or character from the keyboard and depending upon its value select the operation you wish to do
    Code:
    a: Open (filename)
    b: CallF1 (x,y,z)
    c: CallF2 (a,b,c)
    .
    see

    for examples using for, switch, etc

    Comment

    • SnuggleSort
      New Member
      • Mar 2007
      • 3

      #3
      I would suggest using command-line arguments. Look up argc and argv[]. Once you've got the arguments from command-line, it should be trivial to implement a switch statement with each function in the case of its name.

      Comment

      • AmmarN
        New Member
        • Mar 2007
        • 18

        #4
        Originally posted by horace1
        at the simplest level you could read a number or character from the keyboard and depending upon its value select the operation you wish to do
        Code:
        a: Open (filename)
        b: CallF1 (x,y,z)
        c: CallF2 (a,b,c)
        .
        see
        http://www.codeguru.co m/cpp/tic/tic0038.shtml
        for examples using for, switch, etc
        Thanks for your response. Actually its a bit more complicated than that. This is a utility that intakes a binary file and then performs different tasks on it based on what the user decides to do with it, moreover when the user would call a certain action (function) he would also specify many parameters for that function e.g he may wanna specify the range of words he would like to see . I already have all the functions written and the programme is complete. Before I would call the function in the main programme in the order i wanted but now i wanna make completely flexible so the user can call any action from this utlity from the commendline while the programme is running . So what I am looking for is command line statements that would allow me to do this. Therefore i need some help to set up the main function that would accept statements from the command line calling functions with the user provided parameters.

        Thanks for yourtime again.

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          a few questions
          (1) what operating system are you using
          (2) how will you start the program, e.g. from the command line, click an icon, on system start up, on user login, etc
          (3) will the program run in the background or have a window where it is displaying information

          Comment

          • AmmarN
            New Member
            • Mar 2007
            • 18

            #6
            Originally posted by horace1
            a few questions
            (1) what operating system are you using
            (2) how will you start the program, e.g. from the command line, click an icon, on system start up, on user login, etc
            (3) will the program run in the background or have a window where it is displaying information

            I am using linux suse.
            I will start the programme from the command line .
            The programme will display information in the command line window.

            Once again thank you very mcuh for your help and time. I appreciate it alot.

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by AmmarN
              I am using linux suse.
              I will start the programme from the command line .
              The programme will display information in the command line window.

              Once again thank you very mcuh for your help and time. I appreciate it alot.
              will you run if from a terminal window so you can run other programs, e.g. if the program is called prog
              ./prog&

              or so it can use the keyboard, e.g.
              ./prog

              Comment

              • AmmarN
                New Member
                • Mar 2007
                • 18

                #8
                Originally posted by horace1
                will you run if from a terminal window so you can run other programs, e.g. if the program is called prog
                ./prog&

                or so it can use the keyboard, e.g.
                ./prog


                It will be run from the terminal window and i wont run any other prgrams from that window until i quit this program. I hope this makes it clear.Thanks

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by AmmarN
                  It will be run from the terminal window and i wont run any other prgrams from that window until i quit this program. I hope this makes it clear.Thanks
                  The next question is will the program execute a task and then wait for another command - if so that is simple when the currect taks is done you just disply a menu and ask for input.
                  If the program needs to be waiting for input at the same time as processing there are various ways of deal with this.
                  (1) multuthreading - the main thread handles user interaction creating theads to do the tasks as requested - threads can communicate using IPC, e.g. shared memory
                  (2) multiple processes; the parent forks child processes to carry out tasks. The processes can communicate using IPC, e.g. messages, pipes, etc.
                  (3) tatally independent processes communicating via named pipes, TCP, UDP????

                  it all depends what you are trying to do and how interrelated the taks are - multithreading is the simplest

                  Comment

                  • AmmarN
                    New Member
                    • Mar 2007
                    • 18

                    #10
                    Originally posted by horace1
                    The next question is will the program execute a task and then wait for another command - if so that is simple when the currect taks is done you just disply a menu and ask for input.
                    If the program needs to be waiting for input at the same time as processing there are various ways of deal with this.
                    (1) multuthreading - the main thread handles user interaction creating theads to do the tasks as requested - threads can communicate using IPC, e.g. shared memory
                    (2) multiple processes; the parent forks child processes to carry out tasks. The processes can communicate using IPC, e.g. messages, pipes, etc.
                    (3) tatally independent processes communicating via named pipes, TCP, UDP????

                    it all depends what you are trying to do and how interrelated the taks are - multithreading is the simplest


                    Thanks for your reply. Actually the programme will execute the task and then wait for another command. I will try to explain it alittle better this time.Its a Store point binary file that I am trying to show the contents of. My programme is like a editor or a viewer that explains what every byte in that file means. I already know what is the format of the file and how it is created. based on that knowledge i have created my prgramme to show the contents of the file which are more umderstandble than 1s and 0s.

                    So the user will open up the file using my programme and then he may wanna see different parts of the file one by one with different parameters.Fore xample he may start off with the function to display the META DATA of teh file, once that task is done i want my programme to prompt the user to enter the next command or "q" to quit. Now if he wishes to see the Core memory content of the file ,he will call that function and provide parameters (e,g what range of core memory he would like to see) once Core Memory information has been displayed again i want my programme to prompt the user to enter next command or q to quit, so on and so forth.

                    I hope you see what i am trying to do here. one command at a time.I just dont know what is the best way to acheieve this and how to setup the main function.I would have posted the code but its over 1600 lines.
                    Awaiting your reponse.Thanks for your help and time.

                    Comment

                    • horace1
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1510

                      #11
                      here is an example of a very simple menu system which would probably do what you require
                      Code:
                      // very simple command line menu
                      
                      #include <stdio.h>
                      
                      int main()
                      {
                          while(1)   // execute 'forever'
                            {
                             int choice=-1;
                             while(choice <=0 || choice > 5)    // ignore any invalide choice
                                {
                                 printf("Select from command\n"
                                         "  1: Open file\n  2: Process data\n  3: display results\n"
                                         "  4: Save file\n  5:  Exit\n   enter command (1 to 5) ?");
                                 scanf("%d", &choice);
                                 }
                             // valid choice enter, execute command
                             switch (choice)
                                 {
                                 case 1: // statements to open file
                                         printf("Open file\n");
                                         break;
                                 case 2: // statements to process data
                                         printf("Process data\n");
                                         break;
                                 case 3: // statements to display results
                                         printf("display results\n");
                                         break;
                                 case 4: // statements to Save file
                                         printf("Save file\n");
                                         break;
                                 case 5: exit(0);
                                 default:  // should not happen!
                                         printf("Error!!!! %d\n", choice);
                                  }
                             }
                      }
                      the user enter the command between 1 and 5 (you could use characters O, P, D, S, etc) - anything else is ignored. The command selected is executed via a switch() selection (you could have a function for each command here!) . When complete the menu is displayed again, etc, etc.

                      Comment

                      • AmmarN
                        New Member
                        • Mar 2007
                        • 18

                        #12
                        Thanks for your code it was very helpfull. Since i have too many commands that the user can call therefore i am not in a position to show the menu everytime or assign a number to every command. What i was thinking , if i could write something that would allow the user to enter the name of the command every time the previous process is done. I have tried doing this using the following code. But its crashing randomnly, it works sometimes but other times it calls different commands than what enetered. I would really appreciate if you could take a look at it and advise.here is the chunk of the code.

                        Code:
                        int srange=0; //start range
                        int erange=0; //end range
                        int eerange=0; //another end range
                        
                        
                          cout<<"\nSTP:::"; //name of the utlity
                          cin>>command;  //this is where user will enter the command
                        
                           while (command!="q"||"Q") // while not equal to quit
                           {
                             
                             if (command =="SCM") {
                               cout<<"Please enter the Start range:";
                               cin>>oct>>srange;
                               cout<<"Please enter the End range:";
                               cin>>oct>>erange;
                               SCM(srange,erange);  //function
                               cout<<"\nSTP:::";
                               srange=0;
                               erange=0;
                               command="";
                               cin>>command;
                             }
                             else if (command =="SFD") {
                               cout<<"Please enter the Start Sector:";
                               cin>>oct>>srange;
                               cout<<"Please enter the End Sector:";
                               cin>>oct>>erange;
                               SFD(srange,erange);
                               cout<<"\nSTP:::";
                               srange=0;
                               erange=0;
                               command="";
                               cin>>command;
                             }
                             
                             else if (command =="SCDR") {
                               SCDR();
                               cout<<"\nSTP:::";
                               command="";
                               cin>>command;
                             }
                             
                             else if (command =="SDI"){
                               SDI();
                               cout<<"\nSTP:::";
                               command="";
                               cin>>command;
                             }
                             else if (command =="SFP"){
                               SFP();
                               cout<<"\nSTP:::";
                               command="";
                               cin>>command;
                             }
                             else if (command =="SAO"){
                               SAO();
                               cout<<"\nSTP:::";
                               command="";
                               cin>>command;
                             }
                             else if (command =="SDO"){
                               SDO();
                               cout<<"\nSTP:::";
                               command="";
                               cin>>command;
                               }
                               .
                               .
                               .
                              .
                        }//endwhile
                        Last edited by horace1; Mar 28 '07, 04:57 PM. Reason: use code tags

                        Comment

                        • horace1
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 1510

                          #13
                          it looks basically OK except I don't think you can do
                          Code:
                            while (command!="q"||"Q") // while not equal to quit
                          try?
                          Code:
                            while (command!="q"||command!="Q") // while not equal to quit

                          Comment

                          • AmmarN
                            New Member
                            • Mar 2007
                            • 18

                            #14
                            Originally posted by horace1
                            it looks basically OK except I don't think you can do
                            Code:
                              while (command!="q"||"Q") // while not equal to quit
                            try?
                            Code:
                              while (command!="q"||command!="Q") // while not equal to quit
                            Thanks for your reply. I have made the correction that you mentioned but it works either way. Like I mentioned before, its acting very weird, sometimes say if Function A is called it would execute say Function X. Totally random. I cant see a problem with the code or maybe something is wrong with the string comparison. You help is very appreciated. Thank you.

                            Comment

                            • horace1
                              Recognized Expert Top Contributor
                              • Nov 2006
                              • 1510

                              #15
                              I assume your variable command is a C++ string - also check that all your comparisons are == and not =.
                              This little program which look similar to your idea works OK
                              Code:
                              // very simple command line menu
                              
                              #include <iostream>
                              #include <string>
                              using namespace std;
                              
                              int main()
                              {
                                     string command("");;
                                     while(command != "Q" && command !="q")    // exit on Q or q
                                        {
                                         printf("Select from command\n"
                                                 "  Open, Process, Save, Q\n   enter command ?");
                                         cin >> command;
                                         if(command=="Open")    cout << "Open" << endl;  else
                                         if(command=="Process") cout << "Process" << endl;  else
                                         if(command=="Save")    cout << "Save" << endl;  else
                                                                cout << "Illegal command try again" << endl;
                                         }
                              }

                              Comment

                              Working...