Thread: Command line argument parsing issues?

  1. #1
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269

    Command line argument parsing issues?

    Ok I'm on a Windows machine and I'm writing a simple tool to dump and touch ttf files. It's almost done except that the command line parser is giving nightmares.

    My question is that how do I stop the shell from automatically converting wildcards in arguments to directory listings? (I first flatten all arguments to a single string before parsing)

  2. #2
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    I use gcc 3.4.2 (mingw)

    I cant give you the parse code from the ttf tool (it's 500+ lines and full of dependencies) but here's a short code that demonstrates the issue:

    Code:
    /* shelltest.c */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc,char *argv[]) {
    	int i=argc;
    	char **args=argv;
    
    	printf("argc=%i\n",argc);
    
    	while(i--) printf("%s ",*args++);
    
    	return 0;
    }
    The directory listing is:
    Code:
    Directory of D:\touch\test
    
    01/01/2002  05:46 AM    <DIR>          .
    01/01/2002  05:46 AM    <DIR>          ..
    01/01/2002  05:46 AM               197 shelltest.c
    01/01/2002  05:46 AM            15,663 shelltest.exe
    01/01/2002  05:47 AM                 9 some.txt
                   3 File(s)         15,869 bytes
    Example usage:
    Code:
    shelltest *
    gives
    Code:
    argc=4
    shelltest shelltest.c shelltest.exe some.txt
    How do I get the '*' as it is in the arguments?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well you do what you did when you posted, you put the * in either single or double quotes.

    Wildcard expansion (called globbing) is a function of the shell you use to invoke the program, not the program itself.

    So if you want a "*" to be itself, you usually have to quote it in some way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Yes but it's a program for release, it has to be able to handle the situation when the user types in * (without quotes) as an argument without ..........ing up big time. Is there any way to get the * as it is without putting it in quotes?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    On Windows the program itself is responsible for wildcard expansion rather than the shell. If you want to make MinGW stop wildcard expansion, you can set the following global variable. I'm pretty sure it is for MinGW only.

    Code:
    int _CRT_glob = 0;

  6. #6
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Thanks this works! and how do i get the quotation marks as they are in my args. (as i said i find it too complicated to parse directly without flattening.)

    I flatten all args except the program name (argv[0]) to a single string. So ex:

    Code:
    evilfont -i "some font.ttf" --scale 1.5 -o new.ttf
    How do I get the contents of flattened string exactly as:

    Code:
    -i "some font.ttf" --scale 1.5 -o new.ttf

  7. #7
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Wait, I think I work around this issue by actually double-quoting the arg after -i and -o in the flattener. There was another option --loadGlyph char=number filename, I can work around it by changing the argument order to --loadGlyph filename char=number

    But still I'd like to know some way of getting the quotes as they are.

  8. #8
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Yep that trick works for the -i and -o options, I'll change the --loadGlyph option later.

    Thanks for your help c99tutorial and Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line
    By aaleclaire in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2012, 06:54 PM
  2. Command Line Argument Parsing
    By lithium in forum Windows Programming
    Replies: 3
    Last Post: 07-13-2005, 07:01 PM
  3. command argument line
    By Tonyukuk in forum C++ Programming
    Replies: 5
    Last Post: 01-28-2003, 09:10 AM
  4. Command line argument
    By Max in forum C Programming
    Replies: 2
    Last Post: 07-17-2002, 02:10 PM
  5. '*' as a command line argument
    By kooma in forum C Programming
    Replies: 6
    Last Post: 02-26-2002, 02:18 PM

Tags for this Thread