Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > OT: processing command line parameters including delimiters

Reply
Thread Tools

OT: processing command line parameters including delimiters

 
 
hal styli
Guest
Posts: n/a
 
      01-10-2004
/* Hello, can someone please help with a command line parsing problem.

Apologies for this is being a bit off topic as it is specific to winxp (and
perhaps BCC 5)...

I want to treat an argument such as '/x:1,2' as one argument rather than as
the two arguments '/x:1' and '2' (ignore quotes).

Note '/x:1;2' will give also '/x:1' and '2' so simply sticking a comma in
there doesnt quite do it.

Also I could use "/x:1,2" (keeping the double quotes) which would work but
that is not acceptable.

Is there a simple way of achieving this?

Code input and output below should make it clear.

Thanks in advance.

Hal
----------------------------------------------*/
/*showargs*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PATHDELIM '\\'

void main(int argc, char **argv )
{
int i;
char *name,*q;

name=argv[0];

while(q=strchr(name,PATHDELIM))
name=q+1;

printf("\n%s...\n", name);

printf("\n # len argument\n -- --- ------------------\n");

for(i=0; i<argc; i++)
printf("%4d)%4d: %s\n", i, strlen(argv[i]), argv[i]);

printf("---------\nargc = %d\n",argc);

exit(EXIT_SUCCESS);
}

/* -----------------------------------------------------

C:\showargs one two /a:1:2 /b:3,4 /c:5;6

gives:-
-------------------------------------------------------

showargs.exe...

# len argument
-- --- ------------------
0) 43: C:\DOCUME~1\hals\LOCALS~1\Temp\showargs.exe
1) 3: one
2) 3: two
3) 6: /a:1:2
4) 4: /b:3
5) 1: 4
6) 4: /c:5
7) 1: 6
---------
argc = 8
------------------------------------------------------*/



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-10-2004
hal styli wrote:

> I want to treat an argument such as '/x:1,2' as one argument rather than
> as the two arguments '/x:1' and '2' (ignore quotes).
>
> Note '/x:1;2' will give also '/x:1' and '2' so simply sticking a comma in
> there doesnt quite do it.
>
> Also I could use "/x:1,2" (keeping the double quotes) which would work but
> that is not acceptable.
>
> Is there a simple way of achieving this?


Does "write your own shell" count as simple?

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-10-2004
hal styli wrote:
>
> Apologies for this is being a bit off topic as it is specific to
> winxp (and perhaps BCC 5)...
>
> I want to treat an argument such as '/x:1,2' as one argument
> rather than as the two arguments '/x:1' and '2' (ignore quotes).
>
> Note '/x:1;2' will give also '/x:1' and '2' so simply sticking a
> comma in there doesnt quite do it.
>
> Also I could use "/x:1,2" (keeping the double quotes) which would
> work but that is not acceptable.
>
> Is there a simple way of achieving this?


The following with DJGPP and gcc under W98 using 4dos as shell.
The last part is switching to bash as the shell. Then I even
tried command. Get thee to a windows group.

c:\c\junk>gcc args.c

c:\c\junk>a a b c,d e
e
c,d
b
a
c:/c/junk/a.exe

c:\c\junk>a a b c:d e
e
c:d
b
a
c:/c/junk/a.exe

c:\c\junk>type args.c
#include <stdio.h>
int main(int argc, char** argv) {
while (argc --) puts(argv[argc]);
return 0;
}

c:\c\junk>bash
bash-2.04$ ./a a b c,d e
e
c,d
b
a
../a.exe
bash-2.04$ ./a a b c:d e
e
c:d
b
a
../a.exe
bash-2.04$
exit
c:\c\junk>command

Microsoft(R) Windows 98
(C)Copyright Microsoft Corp 1981-1998.

C:\c\junk>a a b c,d e
e
c,d
b
a
c:/c/junk/a.exe

<OT>
Back around MSDOS 3.3 Command had the evil habit of treating
commas as blanks and suppressing them in arguments. Microsoft may
well have gone back to those evil ways in winxp. If so that is
YARTEXP (Yet another reason to eschew XP). Another solution for
you may be to get 4dos or 4nt, which are available from
jpsoft.com, or install bash, or install DJGPP and bash, etc.
</OT>

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      01-10-2004

On Sat, 10 Jan 2004, CBFalconer wrote:
>
> hal styli wrote:
> >
> > Apologies for this is being a bit off topic as it is specific to
> > winxp (and perhaps BCC 5)...
> >
> > I want to treat an argument such as '/x:1,2' as one argument
> > rather than as the two arguments '/x:1' and '2' (ignore quotes).

<snip>
> <OT>
> Back around MSDOS 3.3 Command had the evil habit of treating
> commas as blanks and suppressing them in arguments. Microsoft may
> well have gone back to those evil ways in winxp.


Nope. DJGPP handles arguments exactly as you'd expect, whether
the program is run from CMD or (probably) any other shell. Commas
do not count as spaces.
It may still be a bug-feature in BC 5's argument parsing startup
code, and thus compiler-specific, but it's nothing to do with
Windows. (Or standard C, the topic of this newsgroup, either.)
My advice: Here's no nickel. Get a better compiler.

-Arthur

 
Reply With Quote
 
hal styli
Guest
Posts: n/a
 
      01-10-2004
Im very embarrassed to say it was user error - I had a batch file wrapper
which meesed things up.
I have found BCC to be an excellent compiler when Im fully awake.

However, I havent found a free IDE im happy with - i might try and see if
vim can be configured to act as an IDE...





 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-10-2004
"Arthur J. O'Dwyer" wrote:
>

.... snip ...
>
> Nope. DJGPP handles arguments exactly as you'd expect, whether
> the program is run from CMD or (probably) any other shell.
> Commas do not count as spaces.


On the old MsDos, circa 3.3 or less at least, they did. DJGPP or
anything else would have no chance at recovering the commas,
because the shell input parser converted them to spaces. I
remember this very well, because it frustrated my porting some of
my software from CP/M, where a pair of commas indicated an omitted
parameter.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      01-11-2004
hal styli wrote:

<snip>

> /*showargs*/
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define PATHDELIM '\\'
>
> void main(int argc, char **argv )

^^^^^^^^^

int main(int argc, char **argv)

main returns int in C.

<snip>

-nrk.
 
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
command line processing question News C++ 8 09-17-2007 07:44 AM
Processing command line arguments Gregor H. C Programming 5 04-22-2007 09:50 PM
Command Line Argument Processing robertlaferla@comcast.net Ruby 6 01-11-2007 03:17 AM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM
command line text processing.. Jim Carter Perl Misc 2 11-10-2003 10:27 PM



Advertisments