Skip to content
This repository
tree: 3583a2563a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (84 sloc) 2.832 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/* Program Mode Class
    Each program mode represents a distinct behavioral pattern for the program.
    Each mode parses a different set of arguments and dispatches to a different
    main function.
*/
class Rosella.CommandLine.ProgramMode
{
    // TODO: Provide an automated help feature where each named argument can
    // take a short description which can be printed

    // TODO: Take an integer to use as the default exit code

    // TODO: Take a list of handles to use as the interpreter default during
    // execution

    var name; // The name of the mode
    var func; // The function to call
    var conditions; // The conditions that determine if we use this mode
    var usage; // Usage string for this mode

    // Constructor. Each ProgramMode has a name
    function ProgramMode(string name)
    {
        self.name = name;
    }

    // Set the main function to call when this mode is activated
    function set_function(var func)
    {
        self.func = func;
        return self;
    }

    function set_usage(string usage)
    {
        self.usage = usage;
        return self;
    }

    function get_usage()
    {
        // TODO: Can we try to autogenerate this from the list of conditions,
        // if we don't have one set?
        var usage = self.usage;
        if (usage == null)
            return "";
        return string(usage);
    }

    // Called by the Program class internally. This function determines if the
    // given list of arguments satisfies the list of required arguments for this
    // ProgramMode. If so, the mode will be activated and executed
    function can_accept(var args)
    {
        if (self.conditions == null)
            return true;
        for (var condition in self.conditions) {
            if (!condition.is_satisfied(args))
                return false;
        }
        return true;
    }

    // Get the main function to dispatch to.
    function main_function()
    {
        return self.func;
    }

    function require_flag(string name)
    {
        if (self.conditions == null)
            self.conditions = [];
        var condition = new Rosella.CommandLine.ModeCondition.FlagSet(name);
        push(self.conditions, condition);
        return self;
    }

    function require_value(string name, string value)
    {
        if (self.conditions == null)
            self.conditions = [];
        var condition = new Rosella.CommandLine.ModeCondition.ScalarValue(name, value);
        push(self.conditions, condition);
        return self;
    }

    function require_condition(var f)
    {
        if (self.conditions == null)
            self.conditions = [];
        var condition = new Rosella.CommandLine.ModeCondition.GenericFunc(f);
        push(self.conditions, condition);
        return self;
    }
}
Something went wrong with that request. Please try again.