Results 1 to 9 of 9

Thread: coding a commandLine class

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default coding a commandLine class

    Hello,
    I'm coding a class that can parse the main() parameters; I follow example on Primer 3rd edition; it says to encapsulate the command line option (eg: "nameapplication.exe -f input.txt -t 45 - u 80") into CommandLine class; and write method that can access to it's members; in my case I have one field "string inputFile;". But I notice that, this implementation it much tied to the problem I'm solving(ie: this class seems me not much reusable, does it?).
    Could anyone get me help? (I understood alright the book??)

    thanks.
    Regards

  2. #2
    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: coding a commandLine class

    Aren't you trying to reinvent the wheel? There are classes to do that. There is even a Qt based class available that performs such a task somewhere in the net. You might also look for getopt_long() implementation on Windows (it's a POSIX function, so it might be unavailable on your system). Or you could just iterate over argv and handle the options. I'd suggest the last solution...

    Qt Code:
    1. int main(int argc, char**argv){
    2. for(int i=1;i<argc;i++){
    3. if(!strcmp(argv[i], "-o")){
    4. fileToOpen = argv[++i];
    5. continue;
    6. }
    7. /* ... */
    8. }
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: coding a commandLine class

    Quote Originally Posted by wysota View Post
    Aren't you trying to reinvent the wheel? There are classes to do that. There is even a Qt based class available that performs such a task somewhere in the net. You might also look for getopt_long() implementation on Windows (it's a POSIX function, so it might be unavailable on your system). Or you could just iterate over argv and handle the options. I'd suggest the last solution...

    Qt Code:
    1. int main(int argc, char**argv){
    2. for(int i=1;i<argc;i++){
    3. if(!strcmp(argv[i], "-o")){
    4. fileToOpen = argv[++i];
    5. continue;
    6. }
    7. /* ... */
    8. }
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Hello, I don't understand what you mean fully...I have to parse the parameters. They are few. I prefer code it on my own (exercise!) and I shuldn't fill the main() so I fiund Primer idea (that was mine too) but I notice what I said in post#1.
    Anyway: I see your code; Primer uses for matching the "swiches" eg:
    Qt Code:
    1. for(int i=1;i<argc;i++){
    2. char* pchar = argv[i];
    3. switch (pchar[0]) {
    4. case '-':
    5. switch (pchar[1]) {
    6. case 'f': prepare for file read
    7. case 'o': prepare for............
    8. case 'd': prepare for.............
    9. };
    10. };
    To copy to clipboard, switch view to plain text mode 
    What do you prefer? In my aboce example if much long to code but if I had many option (eg 10) I have to match 10 if in your way; I think the switch is much more strong in this case ( it has to do one only match instead of 10).
    If I had to parse option like "-wwi", with switch this is boring and long......
    What do you say??
    thank you.
    Regards

  4. #4
    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: coding a commandLine class

    It depends if you only have short params or also long params. What you say suggests you also have long params so using strcmp() or std::string::operator== or QString::operator== seems a better choice. An alternative (which I use myself for a different task) is to compute a hash of the argument (using qHash() for example) and then go for switch(). But this might be an overkill if you have just a few arguments. Building a suffix trie of all possible choices is an option as well, but again might be considered an overkill, because you only do the parsing once.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: coding a commandLine class

    Sorry bur referring my post #1, there's no be answer.....Anyone that have read Primer?
    Again: is that class implementation much tied to the problem that I have to solve??
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: coding a commandLine class

    Quote Originally Posted by mickey View Post
    Again: is that class implementation much tied to the problem that I have to solve??
    I think you should implement a generic command line parser. You should tell it what parameters are valid, whether they are optional or not and so on. Using this information such class should be able to parse the command line, detect errors and let you access the values of paramters that were set by the user and default values for the rest.
    Last edited by jacek; 26th November 2007 at 15:39. Reason: typo

  7. #7
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: coding a commandLine class

    We can't solve problems by using the same kind of thinking we used when we created them

  8. #8
    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: coding a commandLine class

    Quote Originally Posted by sunil.thaha View Post
    Quote Originally Posted by wysota
    There is even a Qt based class available that performs such a task somewhere in the net.
    Cheers!

  9. #9
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: coding a commandLine class

    Quote Originally Posted by wysota View Post
    Cheers!
    This is too much
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  2. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  3. Replies: 2
    Last Post: 16th March 2007, 09:04
  4. Replies: 2
    Last Post: 4th May 2006, 19:17
  5. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27

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.