OT: processing command line parameters including delimiters

Discussion in 'C Programming' started by hal styli, Jan 10, 2004.

  1. hal styli

    hal styli Guest

    /* 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), argv);

    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
    ------------------------------------------------------*/
     
    hal styli, Jan 10, 2004
    #1
    1. Advertising

  2. 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
     
    Richard Heathfield, Jan 10, 2004
    #2
    1. Advertising

  3. hal styli

    CBFalconer Guest

    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!
     
    CBFalconer, Jan 10, 2004
    #3
  4. 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
     
    Arthur J. O'Dwyer, Jan 10, 2004
    #4
  5. hal styli

    hal styli Guest

    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...
     
    hal styli, Jan 10, 2004
    #5
  6. hal styli

    CBFalconer Guest

    "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!
     
    CBFalconer, Jan 10, 2004
    #6
  7. hal styli

    nrk Guest

    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.
     
    nrk, Jan 11, 2004
    #7
    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. Jonck van der Kogel
    Replies:
    2
    Views:
    998
    Jonck van der Kogel
    May 27, 2004
  2. Gregor H.

    Processing command line arguments

    Gregor H., Apr 21, 2007, in forum: C Programming
    Replies:
    5
    Views:
    517
  3. News
    Replies:
    8
    Views:
    338
    James Kanze
    Sep 17, 2007
  4. Replies:
    6
    Views:
    140
  5. Jim Carter

    command line text processing..

    Jim Carter, Nov 10, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    98
    Tad McClellan
    Nov 10, 2003
Loading...

Share This Page