Results 1 to 11 of 11

Thread: QCommandLineParser to complex application

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QCommandLineParser to complex application

    Hi,

    I have question about QCommandLineParser
    I read documentation about this class and I don't see support to several arguments with independent options,
    for example in my app I need independent function to filesystem such as

    # move [source] [destination] [options]
    # move C:/first.txt D:/first.txt --information

    # find [filename] [options]
    # find somefile --type=txt --casesensitive=true --recursive=true

    and many many more functions to filesystem and network.

    But QCommandLineParser allow use option only for first argument so if my app name "test.exe" I
    can only write such as

    test.exe --[options] [optionalargument]

    Maybe someone with you know how I can solve this problem?
    My app must works similar a bash shell that is to say it must work in loop and operate with many classes,
    many functions and many arguments with options.

    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: QCommandLineParser to complex application

    QCommandLineParser accepts a list of strings to use as arguments so you can preprocess the list manually and reorder or cut away some entries before passing the list to QCommandLineParser.
    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.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCommandLineParser to complex application

    The example given in the documentation does exactly what our "move" example does.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCommandLineParser to complex application

    Please look for images.

    option --help.pngfind --help.png

    First problem - how adapt "Usage: " information to display only name of application / its alias not full path to program.

    And when I write
    # find --help

    "Usage:" information should looks as
    # Usage: LockCommand find [options]

    How i can change "Usage:" information?

  5. #5
    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: QCommandLineParser to complex application

    Quote Originally Posted by atomic View Post
    How i can change "Usage:" information?
    Maybe you should write your own commandline parser? Apparently QCommandLineParser is not for you, it is suited for standard UNIX syntax cases where options come before positional arguments. Your case is obviously different.
    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.


  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCommandLineParser to complex application

    Quote Originally Posted by wysota View Post
    Apparently QCommandLineParser is not for you, it is suited for standard UNIX syntax cases where options come before positional arguments.
    Options can be after positional arguments.
    I wasn't sure either, so I copied the example from the documentation (the "copy program" one) and tried it myself.
    It is possible to specify the -p option anywhere, before the two positional arguments, in between or after.

    Cheers,
    _

  7. #7
    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: QCommandLineParser to complex application

    Quote Originally Posted by anda_skoa View Post
    Options can be after positional arguments.
    I wasn't sure either, so I copied the example from the documentation (the "copy program" one) and tried it myself.
    It is possible to specify the -p option anywhere, before the two positional arguments, in between or after.
    Ok, but I guess OP wants a specific order of arguments, e.g. with how find works: find <where> -what. You cannot change the order of arguments there for a number of reasons (I mean with this specific tool).
    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.


  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCommandLineParser to complex application

    I don't know, at least that is not what the first posting said.

    It falsely claimed that options had to be passed first, which is not true.

    Cheers,
    _

  9. #9
    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: QCommandLineParser to complex application

    Quote Originally Posted by anda_skoa View Post
    I don't know, at least that is not what the first posting said.

    It falsely claimed that options had to be passed first, which is not true.
    Yes, I know. People don't always say what they really wanted to say
    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.


  10. #10
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCommandLineParser to complex application

    "It falsely claimed that options had to be passed first, which is not true." - You have right. I am noob and
    I mislead.

    I solve my problem with the order arguments and options. Also solve problem with "Usage: " information
    by write my own help function.

    I have last question about this post.
    I started with this code

    .h
    Qt Code:
    1. #ifndef COMMANDLINEMANAGER_H
    2. #define COMMANDLINEMANAGER_H
    3.  
    4. #include <QObject>
    5. #include <QCommandLineParser>
    6. #include <QCommandLineOption>
    7. #include <QTextStream>
    8. #include <QString>
    9.  
    10. #include <QDebug>
    11.  
    12.  
    13. class CommandLineManager : public QObject
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit CommandLineManager(QObject *parent = 0);
    18.  
    19. public slots:
    20. void readInput();
    21.  
    22. private:
    23.  
    24.  
    25. };
    26.  
    27. #endif // COMMANDLINEMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include "commandlinemanager.h"
    2.  
    3. CommandLineManager::CommandLineManager(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    7.  
    8. void CommandLineManager::readInput()
    9. {
    10. while( 1 ) {
    11.  
    12. QTextStream( stdout )<< "LockCommand" <<endl;
    13. QTextStream( stdout )<< "# ";
    14.  
    15. QTextStream qin( stdin );
    16. QStringList cmd = QCoreApplication::arguments()<< qin.readLine().split( " " );
    17.  
    18. QCommandLineParser parser;
    19. parser.addHelpOption();
    20. parser.addPositionalArgument("command", "The command to execute." );
    21.  
    22. parser.parse( cmd );
    23.  
    24. const QStringList args = parser.positionalArguments();
    25. const QString command = args.isEmpty() ? QString() : args.first();
    26. if (command == "open") {
    27.  
    28. // best way to do this function?
    29. // emit open( arguments and options ); ?
    30. }
    31. if( command == "copy" ) {
    32. // check next option
    33. }
    34.  
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    main
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QCoreApplication::setApplicationName( "LockCommand" );
    6. QCoreApplication::setApplicationVersion( "1.0" );
    7.  
    8. CommandLineManager commandLineManager;
    9. QMetaObject::invokeMethod( &commandLineManager, "readInput", Qt::QueuedConnection );
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    I must use invokeMethod otherwise my readInput() function never allow to call app.exe().
    Well I started?

    And please tell me what is the best way to managment calls specific class /methods
    for example when i read "open" with some arguments and options

    I can connect in constructor with MyFileSystem class and then emit signal
    if (command == "open") {
    // best way to do this function?
    // emit open( arguments and options ); ?
    }

    but if I will have np 50 functions I will have 50 if statement - it is good project?
    Regards,

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCommandLineParser to complex application

    This looks more like an approach for a factory to me.

    You have a QCommandlineParser parse the initial commandline and extract the command name from the first positional argument, maybe handle any options that are not command specific if you have any.

    Then you take the command and hand it to a factory class which instantiates a handler for that command.
    This command handler can then use its own QCommandlineParser to parse the command specific arguments, even provide command specific help.

    Cheers,
    _

Similar Threads

  1. Complex Numbers Problem
    By xtlc in forum Newbie
    Replies: 5
    Last Post: 17th January 2014, 15:34
  2. Complex number
    By nbo10 in forum Qt Programming
    Replies: 1
    Last Post: 4th April 2009, 06:44
  3. complex calculations with qt
    By gt.beta2 in forum Newbie
    Replies: 5
    Last Post: 7th February 2009, 23:55
  4. Complex QCompleter
    By Amouse in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2008, 22:39
  5. Complex Numbers
    By Max Yaffe in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2007, 17:40

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.