Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/doc/ArgumentCompletionFunctions


Ignore:
Timestamp:
Oct 7, 2008, 2:49:10 PM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/ArgumentCompletionFunctions

    v1 v1  
     1= ArgumentCompletionFunctions =
     2[[TracNav(TracNav/TOC_Development)]]
     3
     4== Description ==
     5An ArgumentCompletionFunction returns an [wiki:ArgumentCompletionList] used by the [wiki:CommandExecutor] to complete arguments of a [wiki:ConsoleCommand]. To bind an ArgumentCompletionFunction to an argument, an [wiki:ArgumentCompletor] is use.
     6
     7== Arguments ==
     8An ArgumentCompletionFunction may take several parameters. The first parameter is the currently given argument fragment, the second parameter is the last finished argument, the third parameter is the second last finished argument etc.
     9
     10Example:
     11
     12There are currently 3 finished arguments and one fragment. The user wants a list of the possible arguments for the fourth argument:
     13{{{
     14> command-name argument1 argument2 argument3 fragment
     15}}}
     16
     17Then the ArgumentCompletionFunction of the fourth argument gets called the following way:
     18{{{
     19ac_function(fragment, argument3, argument2, argument1);
     20}}}
     21
     22Of course the function doesn't necessarily has to take all arguments, it's possible to just use the fragment or to use no arguments at all.
     23
     24== Usage ==
     25=== Defining a new ArgumentCompletionFunction ===
     26In {{{core/ArgumentCompletionFunctions.h}}} write:
     27{{{
     28ARGUMENT_COMPLETION_FUNCTION_DECLARATION(functionname)  \
     29                         (const std::string& fragment,  \
     30                          const std::string& argument1, \
     31                          const std::string& argument2, \
     32                          ...);
     33}}}
     34
     35=== Implementing a new ArgumentCompletionFunction ===
     36In {{{core/ArgumentCompletionFunctions.cc}}} write:
     37{{{
     38ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(functionname)  \
     39                            (const std::string& fragment,  \
     40                             const std::string& argument1, \
     41                             const std::string& argument2, \
     42                          ...);
     43{
     44    ArgumentCompletionList myList;
     45
     46    for (some_condition)
     47    {
     48        std::string argument, lowercase, display;
     49
     50        argument  = some_logic1();
     51        lowercase = some_logic2();
     52        display   = some_logic3();
     53
     54        ArgumentCompletionListElement element(argument, lowercase, display)
     55        myList.push_back(element);
     56    }
     57
     58    return myList;
     59}
     60}}}
     61Note: ''lowercase'' and ''display'' in the constructor of ArgumentCompletionListElement are optional. If not specified, the first string (''argument'') is used.
     62
     63== Example ==
     64The following example implements an !ArgumentCompletionList returning a set of possible names for girls or boys:
     65
     66In {{{core/ArgumentCompletionFunctions.h}}}:
     67{{{
     68ARGUMENT_COMPLETION_FUNCTION_DECLARATION(children_names) \
     69    (const std::string& fragment, const std::string& gender);
     70}}}
     71
     72In {{{core/ArgumentCompletionFunctions.cc}}}:
     73{{{
     74ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(children_names) \
     75    (const std::string& fragment, const std::string& gender)
     76{
     77    ArgumentCompletionList myList;
     78
     79    if (gender == "boy")
     80    {
     81        myList.push_back(ArgumentCompletionListElement("Bob", "bob"));
     82        myList.push_back(ArgumentCompletionListElement("John", "john"));
     83        myList.push_back(ArgumentCompletionListElement("Elton", "elton"));
     84        myList.push_back(ArgumentCompletionListElement("Bruce", "bruce"));
     85    }
     86    else if (gender == "girl")
     87    {
     88        myList.push_back(ArgumentCompletionListElement("Jessica", "jessica"));
     89        myList.push_back(ArgumentCompletionListElement("Amanda", "amanda"));
     90        myList.push_back(ArgumentCompletionListElement("Kate", "kate"));
     91        myList.push_back(ArgumentCompletionListElement("Angelina", "angelina"));
     92    }
     93
     94    return myList;
     95}
     96}}}
     97
     98Now it's possible to complete names:
     99{{{
     100> command-name boy
     101Possible arguments: Bob, Bruce, Elton, John
     102
     103> command-name boy b
     104Possible arguments: Bob, Bruce
     105
     106> command-name boy bo
     107Possible arguments: Bob
     108
     109> command-name girl je
     110Possible arguments: Jessica
     111
     112> command-name girl jennif
     113Possible arguments: -
     114}}}