@@ -98,25 +98,32 @@ def c_if_option(o):
98
98
c_name (o .long or o .short ))
99
99
100
100
101
- def parse_leafs (pattern ):
101
+ def parse_leafs (pattern , all_options ):
102
+ options_shortcut = False
102
103
leafs = []
103
104
queue = [(0 , pattern )]
104
105
while queue :
105
106
level , node = queue .pop (- 1 ) # depth-first search
106
- if hasattr (node , 'children' ):
107
+ if not options_shortcut and type (node ) == docopt .OptionsShortcut :
108
+ options_shortcut = True
109
+ elif hasattr (node , 'children' ):
107
110
children = [((level + 1 ), child ) for child in node .children ]
108
111
children .reverse ()
109
112
queue .extend (children )
110
113
else :
111
114
if node not in leafs :
112
115
leafs .append (node )
113
- leafs .sort (key = lambda e : e .name )
116
+ sort_by_name = lambda e : e .name
117
+ leafs .sort (key = sort_by_name )
114
118
commands = [leaf for leaf in leafs if type (leaf ) == docopt .Command ]
115
119
arguments = [leaf for leaf in leafs if type (leaf ) == docopt .Argument ]
116
- flags = [leaf for leaf in leafs
117
- if type (leaf ) == docopt .Option and leaf .argcount == 0 ]
118
- options = [leaf for leaf in leafs
119
- if type (leaf ) == docopt .Option and leaf .argcount > 0 ]
120
+ if options_shortcut :
121
+ option_leafs = all_options
122
+ option_leafs .sort (key = sort_by_name )
123
+ else :
124
+ option_leafs = [leaf for leaf in leafs if type (leaf ) == docopt .Option ]
125
+ flags = [leaf for leaf in option_leafs if leaf .argcount == 0 ]
126
+ options = [leaf for leaf in option_leafs if leaf .argcount > 0 ]
120
127
leafs = [i for sl in [commands , arguments , flags , options ] for i in sl ]
121
128
return leafs , commands , arguments , flags , options
122
129
@@ -148,9 +155,9 @@ def parse_leafs(pattern):
148
155
if isinstance (usage , list ):
149
156
raise docopt .DocoptLanguageError ('' .join (usage ))
150
157
151
- options = docopt .parse_defaults (doc )
152
- pattern = docopt .parse_pattern (docopt .formal_usage (usage ), options )
153
- leafs , commands , arguments , flags , options = parse_leafs (pattern )
158
+ all_options = docopt .parse_defaults (doc )
159
+ pattern = docopt .parse_pattern (docopt .formal_usage (usage ), all_options )
160
+ leafs , commands , arguments , flags , options = parse_leafs (pattern , all_options )
154
161
155
162
t_commands = ';\n ' .join ('int %s' % c_name (cmd .name )
156
163
for cmd in commands )
0 commit comments