Results 1 to 3 of 3

Thread: command line parameters: wildcard problem

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default command line parameters: wildcard problem

    Hey guys,

    I'm working on a C++ program that runs/processes data from a bash shell on Cygwin. I've written code to parse command line arguments like so:

    Qt Code:
    1. // in main(int argc, char *argv[])
    2. char testFile[10][256]; // holds up to 10 testFile names of up to 256 char each
    3. uint16 testFileNUM = 0;
    4.  
    5.  
    6. // call my parsing function:
    7. doArgs(argc, argv);
    8.  
    9.  
    10. // afterward report the parameter info
    11. for(uint16 a=0; a<testFileNUM; a++)
    12. printf("TestFile and NUM: %s %hd\n", testFile[a], testFileNUM);
    13.  
    14.  
    15. // snippet from command line parsing function
    16. void doArgs(int argc, char *argv[])
    17. {
    18. char *a;
    19. while(--argc > 0 && (*++argv)[0] == '-')
    20. {
    21. a = argv[0] + 1;
    22. switch(*a)
    23. {
    24. case 't':
    25. if(!strcasecmp(a, "test_file"))
    26. {
    27. sscanf(*++argv, "%s", &testFile[testFileNUM]);
    28. testFileNUM++;
    29. argc--;
    30. }
    31. else
    32. {
    33. printf("Unknown option: %s\n", *argv);
    34. usage();
    35. exit(1);
    36. }
    37. break;
    38. // etc...
    39. }
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    This has worked fine for me. If I type:

    $> program -test_file text01.txt -test_file text02.txt -test_file text03.txt

    I get the following expected report:

    $> TestFile and NUM: text01.txt 3
    $> TestFile and NUM: text02.txt 3
    $> TestFile and NUM: text03.txt 3


    This is what I want: after the file names have been read into the program, I can open them and do what I want.

    However, now I've a situation such that, instead of specifying the input files specifically, I want to read them using a wildcard like so:

    $> program -test_file text*.txt

    But when I do so, I get the following report:

    $> TestFile and NUM: text*.txt 1


    This is useless to me (I think): I cannot keep track of and access the individual files. Can someone give me a suggestion here: how can I read the individual file names in my doArgs() function using the * wildcard?

    Thanks!

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: command line parameters: wildcard problem

    well yes - c++ is not a mind reader.

    you have to read the local directory and use regex to match filenames.

    btw, boost :: program_options might be of interest. qt may have alternative as well.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    vonCZ (20th April 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: command line parameters: wildcard problem

    '*' should be expanded by the shell. Apparently you're using one that doesn't expand wildcards.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 7
    Last Post: 27th March 2013, 03:40
  2. How to compile from command line?
    By tonnot in forum Newbie
    Replies: 6
    Last Post: 15th March 2011, 14:55
  3. QProcess and the command line
    By auba in forum Qt Programming
    Replies: 17
    Last Post: 27th May 2009, 10:39
  4. Printing to Command Line
    By seanmu13 in forum Qt Programming
    Replies: 3
    Last Post: 5th July 2007, 15:57
  5. Is there a command-line window in Qt4?
    By miaoliang in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2006, 08:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.