Skip to content
This repository
Browse code

[CommandLine] Fix automated help message generation

  • Loading branch information...
commit cccf82db9378dd7069bdb4e148b11456c9cbf0a0 1 parent 3583a25
Andrew Whitworth authored August 28, 2012
13  src/commandline/ArgumentDef.winxed
@@ -24,15 +24,22 @@ class Rosella.CommandLine.ArgumentDef
24 24
     function __get_description_hash(var raw_defs, var sb)
25 25
     {
26 26
         int max = 0;
  27
+        var fixed_defs = {};
27 28
         for (string arg in raw_defs) {
28  
-            int len = length(arg);
  29
+            var parts = split("=", arg);
  30
+            string arg_name = parts[0];
  31
+            string prefix = (length(arg_name) == 1) ? "-" : "--";
  32
+            arg_name = prefix + arg;
  33
+            fixed_defs[arg_name] = raw_defs[arg];
  34
+
  35
+            int len = length(arg_name);
29 36
             if (len > max)
30 37
                 max = len;
31 38
         }
32 39
         max += 4;
33 40
 
34  
-        for (string arg in raw_defs) {
35  
-            string desc = raw_defs[arg];
  41
+        for (string arg in fixed_defs) {
  42
+            string desc = fixed_defs[arg];
36 43
             push(sb, "    ");
37 44
 
38 45
             // TODO: add dashes. -X and --xxx instead of X and xxx
32  src/commandline/ModeCondition.winxed
@@ -4,6 +4,10 @@ class Rosella.CommandLine.ModeCondition
4 4
     {
5 5
         return false;
6 6
     }
  7
+
  8
+    function get_description(var sb)
  9
+    {
  10
+    }
7 11
 }
8 12
 
9 13
 class Rosella.CommandLine.ModeCondition.FlagSet : Rosella.CommandLine.ModeCondition
@@ -21,6 +25,13 @@ class Rosella.CommandLine.ModeCondition.FlagSet : Rosella.CommandLine.ModeCondit
21 25
         int value = args[flagname];
22 26
         return value;
23 27
     }
  28
+
  29
+    function get_description(var sb)
  30
+    {
  31
+        string flagname = self.flagname;
  32
+        push(sb, " ");
  33
+        push(sb, flagname);
  34
+    }
24 35
 }
25 36
 
26 37
 class Rosella.CommandLine.ModeCondition.ScalarValue : Rosella.CommandLine.ModeCondition
@@ -43,15 +54,27 @@ class Rosella.CommandLine.ModeCondition.ScalarValue : Rosella.CommandLine.ModeCo
43 54
             return true;
44 55
         return false;
45 56
     }
  57
+
  58
+    function get_description(var sb)
  59
+    {
  60
+        string name = self.name;
  61
+        string value = self.value;
  62
+        push(sb, " ");
  63
+        push(sb, name);
  64
+        push(sb, "=");
  65
+        push(sb, value);
  66
+    }
46 67
 }
47 68
 
48 69
 class Rosella.CommandLine.ModeCondition.GenericFunc : Rosella.CommandLine.ModeCondition
49 70
 {
50 71
     var func;
  72
+    var desc;
51 73
 
52  
-    function GenericFunc(var f)
  74
+    function GenericFunc(var f, string desc)
53 75
     {
54 76
         self.func = f;
  77
+        self.desc = desc;
55 78
     }
56 79
 
57 80
     function is_satisfied(var args)
@@ -59,4 +82,11 @@ class Rosella.CommandLine.ModeCondition.GenericFunc : Rosella.CommandLine.ModeCo
59 82
         var f = self.func;
60 83
         return f(args);
61 84
     }
  85
+
  86
+    function get_description(var sb)
  87
+    {
  88
+        string desc = self.desc;
  89
+        push(sb, " ");
  90
+        push(sb, desc);
  91
+    }
62 92
 }
20  src/commandline/Program.winxed
@@ -20,7 +20,7 @@ class Rosella.CommandLine.Program
20 20
     {
21 21
         self.program_name = program_name;
22 22
         self.modes = {};
23  
-        self.default_mode = new Rosella.CommandLine.ProgramMode("default");
  23
+        self.default_mode = new Rosella.CommandLine.ProgramMode.Default();
24 24
         self.on_error = function(var e) {
25 25
             say("Error parsing arguments");
26 26
             if (e != null)
@@ -115,25 +115,11 @@ class Rosella.CommandLine.Program
115 115
         var sb = new 'StringBuilder';
116 116
         string name = self.program_name;
117 117
 
118  
-        string usage = self.default_mode.get_usage();
119  
-        if (usage == null || usage == "")
120  
-            usage = "<OPTIONS>";
121  
-        push(sb, "Usage: ");
122  
-        push(sb, name);
123  
-        push(sb, " ");
124  
-        push(sb, usage);
125  
-        push(sb, "\n");
  118
+        self.default_mode.get_usage(name, sb);
126 119
 
127 120
         for (string modename in self.modes) {
128 121
             var mode = self.modes[modename];
129  
-            usage = mode.get_usage();
130  
-            if (usage != null && usage != "") {
131  
-                push(sb, "Usage: ");
132  
-                push(sb, name);
133  
-                push(sb, " ");
134  
-                push(sb, usage);
135  
-                push(sb, "\n");
136  
-            }
  122
+            mode.get_usage(name, sb);
137 123
         }
138 124
         if (self.description != null) {
139 125
             string desc = self.description;
62  src/commandline/ProgramMode.winxed
@@ -37,14 +37,23 @@ class Rosella.CommandLine.ProgramMode
37 37
         return self;
38 38
     }
39 39
 
40  
-    function get_usage()
  40
+    function get_usage(string program_name, var sb)
41 41
     {
42  
-        // TODO: Can we try to autogenerate this from the list of conditions,
43  
-        // if we don't have one set?
44 42
         var usage = self.usage;
45  
-        if (usage == null)
46  
-            return "";
47  
-        return string(usage);
  43
+        if (usage != null) {
  44
+            push(sb, "       ");
  45
+            push(sb, program_name);
  46
+            push(sb, " ");
  47
+            push(sb, string(usage));
  48
+            push(sb, "\n");
  49
+        }
  50
+        else if (self.conditions != null) {
  51
+            push(sb, "       ");
  52
+            push(sb, program_name);
  53
+            for (var condition in self.conditions)
  54
+                condition.get_description(sb);
  55
+            push(sb, " <OPTIONS>\n");
  56
+        }
48 57
     }
49 58
 
50 59
     // Called by the Program class internally. This function determines if the
@@ -94,3 +103,44 @@ class Rosella.CommandLine.ProgramMode
94 103
         return self;
95 104
     }
96 105
 }
  106
+
  107
+class Rosella.CommandLine.ProgramMode.Default : Rosella.CommandLine.ProgramMode
  108
+{
  109
+    function Default() { self.name = ""; }
  110
+
  111
+    function get_usage(string program_name, var sb)
  112
+    {
  113
+        var usage = self.usage;
  114
+        if (usage != null) {
  115
+            push(sb, "Usage: ");
  116
+            push(sb, program_name);
  117
+            push(sb, " ");
  118
+            push(sb, string(usage));
  119
+            push(sb, "\n");
  120
+        } else {
  121
+            push(sb, "Usage: ");
  122
+            push(sb, program_name);
  123
+            push(sb, " <OPTIONS>\n");
  124
+        }
  125
+    }
  126
+
  127
+    function require_flag(string name)
  128
+    {
  129
+        Rosella.Error.error("May not add prerequisite conditions to the default program mode");
  130
+    }
  131
+
  132
+    function require_value(string name, string value)
  133
+    {
  134
+        Rosella.Error.error("May not add prerequisite conditions to the default program mode");
  135
+    }
  136
+
  137
+    function require_condition(var f)
  138
+    {
  139
+        Rosella.Error.error("May not add prerequisite conditions to the default program mode");
  140
+    }
  141
+
  142
+    function can_accept(var args)
  143
+    {
  144
+        return true;
  145
+    }
  146
+}

0 notes on commit cccf82d

Please sign in to comment.
Something went wrong with that request. Please try again.