Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to alter argc and argv

Reply
Thread Tools

How to alter argc and argv

 
 
Billy Patton
Guest
Posts: n/a
 
      05-31-2005
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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Heinz Ozwirk
Guest
Posts: n/a
 
      05-31-2005

"Billy Patton" <(E-Mail Removed)> schrieb im Newsbeitrag
news(E-Mail Removed) l.design.ti.com...
>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


 
Reply With Quote
 
 
 
 
Puppet_Sock
Guest
Posts: n/a
 
      06-01-2005
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

 
Reply With Quote
 
msalters
Guest
Posts: n/a
 
      06-02-2005


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

 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      06-03-2005
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.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-03-2005
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Alter - Perl extension for Alter Ego Objects anno4000@radom.zrz.tu-berlin.de Perl Misc 0 06-30-2007 04:13 PM
use of argc and argv Claudio Varini C++ 3 10-28-2004 02:52 PM
argc and argv userfriendly C++ 3 03-01-2004 03:12 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM



Advertisments