Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Getting a program to accept input before executing - Yes, it's a newbie question.

  1. #1
    Join Date
    May 2006
    Location
    South Africa
    Beans
    1,056
    Distro
    Ubuntu 10.04 Lucid Lynx

    Getting a program to accept input before executing - Yes, it's a newbie question.

    Hi,

    I don't know what it's called so could someone please tell me and I'll happily go search Google for it!

    when you in the terminal do a command:

    rsync -ruv --delete

    What do you call the bold bits? How do you write a c++ application to accept those extra "flags"/"options". I've tried some google searches but ended up with a lot of spam.

    Thanks for any pointers,

    Rudolf
    My philosophy in life: If it's not broken, tweak it 'till it breaks.

  2. #2
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    Command line arguments, and they are passed as parameters to the main method. In

    Code:
    int main(int argc, char **argv)
    argv is an array of strings which are the command line args, and argc is the number of said arguments.
    Last edited by CptPicard; April 27th, 2008 at 03:24 PM. Reason: Oops, thanks WW.
    LambdaGrok. | #ubuntu-programming on FreeNode

  3. #3
    Join Date
    Mar 2008
    Beans
    136
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    hi...
    It's called a command-line argument/option.
    "-r" is called a short option , "--recursive" is called (guess what ?) a long option .

    There are two ways to use cmd args:

    1- parse the arguments yourself (argc holds the number of args and argv is an array of char* to the args) just like CptPicard said.

    2- use getopt to parse the args for you, say you want to make a program that accepts -h for help, -v for verbose and -w for width, you use it like this:

    PHP Code:
        const char *opts="w:hv"// w: means w takes an argument, for example -w 500
        
    char opt;

        while( (
    opt=getopt(argcargvopts))){
            switch(
    opt){
                case 
    '?':{
                    
    //unknown option
                    
    exit(1);
                }
                case 
    'h':{
                    
    //print help
                    
    printf("%s\n"help_mesg);
                    exit(
    0);
                    break;
                }
                case 
    'v':{
                    
    //be verbose
                    
    VERBOSE=1;
                    break;
                }
                case 
    'w':{
                    
    //w's argument in optarg
                    
    sscanf(optarg"%d", &width);
                    break;
                }    
            }
        } 
    to parse long options read the manual
    man 3 getopt
    man 3 getopt_long
    "Oh, yeah, real smart. Let's go poking around inside a pod that's probably carrying a half-dozen miniature face-hugging, saliva-dripping alien piranha things. And while we're at it, let's split up so that we're all alone and defenseless, okay?" SQ6.

  4. #4
    Join Date
    Sep 2006
    Location
    BC, Canada
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    There are libraries (can't think of the name off the top of my head) for command-line parsing of arguments. Here is a very basic example:

    C
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
      if( argc != 2 )
        printf("Usage: <program_name> <argument1>\n");
      else
        printf("You typed '%s' as the argument to this program!\n", argv[1]);
    
      return 0;
    }

    C++
    Code:
    #include <iostream>
    
    int main(int argc, char** argv)
    {
      if( argc != 2 )
        std::cout << "Usage: <program_name> <argument1>\n";
      else
        std::cout << "You typed '" << argv[1] <<
           "' as the argument to this program!\n";
    
      return 0;
    }
    You can find a more complete example on the GNU Manual site: http://www.gnu.org/software/libtool/...mple-of-Getopt
    Last edited by JupiterV2; April 26th, 2008 at 06:01 PM. Reason: More info.

  5. #5
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    For the record, a week ago I was checking "Python in a nutshell", and found an interesting library called "optparse", which, does exactly that.

    PHP Code:
    #!/usr/bin/env python

    import optparse

    def init
    ():
        
    optparse.OptionParser()
        
    p.add_option("-l""--long"action="store_true")
        
    p.add_option("-n""--name", default="there")
        
        
    opts,args p.parse_args()
        
        if 
    opts.long:
            print 
    "I welcome you",opts.name
        
    else:
            print 
    "Hello"opts.name

    if __name__ == "__main__":
        
    init() 
    Code:
    python optsample.py -nWorld
    Hello World
    Code:
    python optsample.py --long -nWorld
    I welcome you  World
    Code:
    python optsample.py --help
    Usage: optsample.py [options]
    
    Options:
      -h, --help            show this help message and exit
      -l, --long            
      -n NAME, --name=NAME
    Maybe there's an optparse for c++?
    Last edited by Can+~; April 26th, 2008 at 07:19 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  6. #6
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    OP: no offense, but as such green newbie, you will be **much** better off to start learning with Python and not C++. See wiki in my sig for links, see sticky FAQ for discussion and poll why.

  7. #7
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    Quote Originally Posted by Can+~ View Post
    Maybe there's an optparse for c++?
    Use what is available for C, it will work the same I imagine.

    Quote Originally Posted by pmasiar View Post
    OP: no offense, but as such green newbie, you will be **much** better off to start learning with Python and not C++. See wiki in my sig for links, see sticky FAQ for discussion and poll why.
    Anything but C++ IMO.

  8. #8
    Join Date
    May 2006
    Location
    South Africa
    Beans
    1,056
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    Thanks so much for all the replies guys! I'm used to getting flamed once or twice a year in this part of the forums!

    C++ is what the guys at varsity teach in Software 1, and it gets taken further next year in Software 2. I think they start off with C++ as later in the year the IDE to the PIC controllers work with C.

    Anyway, for the sake of peace I won't ask what you guys have against C++. And as soon as this term's over I'll take your advice on Python to heart!

    Thanks again for all the detailed replies, I really appreciate the time that went in to them!

    Cheers,

    Rudolf
    My philosophy in life: If it's not broken, tweak it 'till it breaks.

  9. #9
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    Quote Originally Posted by RudolfMDLT View Post
    Anyway, for the sake of peace I won't ask what you guys have against C++. And as soon as this term's over I'll take your advice on Python to heart!
    See the sticky on the Holy Wars thread...

  10. #10
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: Getting a program to accept input before executing - Yes, it's a newbie question.

    Quote Originally Posted by CptPicard View Post
    Command line arguments, and they are passed as parameters to the main method. In

    Code:
    int main(char **argv, int argc)
    argv is an array of strings which are the command line args, and argc is the number of said arguments.
    CptPicard, you might want to fix your post. It should be argc first.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •