How to alter argc and argv

Discussion in 'C++' started by Billy Patton, May 31, 2005.

  1. Billy Patton

    Billy Patton Guest

    I need to parse argv to remove known arguments from the array.
    But I want it to leave the rest intact. I need to pass these through to other
    system calls.

    I've found references to several libraries but have been unable to get any to
    compile.

    What I would like is somethhing like:

    $prog -a 1 -b 2 -c 3

    INside program:
    int main(int argc,char** argv)
    {
    do_args(argc,argv);
    argv now is -c 3 because I knew about and wanted -a and -b

    }

    do_args(int& argc,char**& argv)
    {
    switch ()
    {
    case '-a': reduce from argv and argc
    case '-b':
    default :
    if (pass_thru == 0)
    {
    print gotta argument we don't know about
    }
    else
    {
    print argument '%s' not accounted for passing thru\n"
    }
    }

    perl does this in GetOpt::Long

    ___ _ ____ ___ __ __
    / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    /___/
    Texas Instruments ASIC Circuit Design Methodology Group
    Dallas, Texas, 214-480-4455,
     
    Billy Patton, May 31, 2005
    #1
    1. Advertising

  2. Billy Patton

    Heinz Ozwirk Guest

    "Billy Patton" <> schrieb im Newsbeitrag
    news:p...
    >I need to parse argv to remove known arguments from the array.
    > But I want it to leave the rest intact. I need to pass these through to
    > other system calls.
    >
    > I've found references to several libraries but have been unable to get any
    > to compile.
    >
    > What I would like is somethhing like:
    >
    > $prog -a 1 -b 2 -c 3
    >
    > INside program:
    > int main(int argc,char** argv)
    > {
    > do_args(argc,argv);
    > argv now is -c 3 because I knew about and wanted -a and -b
    >
    > }
    >
    > do_args(int& argc,char**& argv)


    // Never tested something like it, but what about this (leaving out lots of
    detail):

    {
    char** in = argv + 1;
    char** out = argv + 1;
    while (*in)
    {
    if (i_know_what_to_do_with(*in))
    {
    do_something_with(*in);
    ++in;
    }
    else
    {
    *out++ = *in++;
    }
    }
    *in = 0;
    }

    HTH
    Heinz
     
    Heinz Ozwirk, May 31, 2005
    #2
    1. Advertising

  3. Billy Patton

    Puppet_Sock Guest

    Billy Patton wrote:
    > I need to parse argv to remove known arguments from the array.
    > But I want it to leave the rest intact. I need to pass these through to other
    > system calls.

    [snip]

    I'm not sure if it will cause problems, but I would not modify
    the actual argc and argv data. I'd take a copy of them and
    only modify that. You are not likely to have a huge amount
    of data involved in these items, and you can put it in any
    convenient collection, such as a standard library container.
    Then you can parse it using all the good C++ stuff you've
    grown to know and love, like iterators and so on.
    Socks
     
    Puppet_Sock, Jun 1, 2005
    #3
  4. Billy Patton

    msalters Guest

    Billy Patton schreef:
    > I need to parse argv to remove known arguments from the array.
    > But I want it to leave the rest intact. I need to pass these through to other
    > system calls.


    No problem, char** argv is a writable array of char*s. Simply copy
    argv[2] to argv[1], argv[3] to argv[2] etc to erase argv[1]. You can
    eve use <algorithm>.

    HTH,
    Michiel Salters
     
    msalters, Jun 2, 2005
    #4
  5. Billy Patton

    Alan Johnson Guest

    Billy Patton wrote:
    > I need to parse argv to remove known arguments from the array.
    > But I want it to leave the rest intact. I need to pass these through to
    > other system calls.
    >


    I suggest disposing of all of the unsafeness (or at least annoyance) of
    dealing directly with argc and argv. You can do so fairly easily with
    one line of code :

    std::vector<std::string> args(argv, argv+argc) ;

    You now have your own private copy of all of the command line arguments
    that you can safely play with as much as you want.
     
    Alan Johnson, Jun 3, 2005
    #5
  6. Billy Patton

    red floyd Guest

    Alan Johnson wrote:
    > std::vector<std::string> args(argv, argv+argc) ;


    Damn, that's clever. Sometimes the obvious is right in front of you and
    you miss it!
     
    red floyd, Jun 3, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. userfriendly

    argc and argv

    userfriendly, Feb 29, 2004, in forum: C++
    Replies:
    3
    Views:
    6,053
    Buster
    Mar 1, 2004
  2. Claudio Varini

    use of argc and argv

    Claudio Varini, Oct 28, 2004, in forum: C++
    Replies:
    3
    Views:
    2,184
    Howard
    Oct 28, 2004
  3. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  4. ern
    Replies:
    2
    Views:
    439
    Walter Roberson
    Aug 17, 2006
  5. -berlin.de
    Replies:
    0
    Views:
    162
    -berlin.de
    Jun 30, 2007
Loading...

Share This Page