How to alter argc and argv

B

Billy Patton

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, (e-mail address removed)
 
H

Heinz Ozwirk

Billy Patton said:
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
 
P

Puppet_Sock

Billy said:
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
 
M

msalters

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
 
A

Alan Johnson

Billy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top