]>
jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/pseudoclient/sys_command.py
7 class CommandManager(object):
9 self
.prefix
= self
.get_prefix()
10 self
.invalid
= self
.get_invalid()
11 self
.commands
= self
.get_commands()
17 def get_invalid(self
):
20 def get_commands(self
):
23 def get_command(self
, command
):
24 command
= command
.lower()
26 if not command
.startswith(self
.prefix
):
29 command
= command
[len(self
.prefix
):]
31 if not command
in self
.commands
:
34 command
= self
.commands
[command
]
36 if not isinstance(command
, basestring
):
39 command
= command
.lower()
41 if not command
in self
.commands
:
44 return self
.commands
[command
]
46 def get_help(self
, command
= None):
50 if command
.startswith(self
.prefix
):
51 command
= command
[len(self
.prefix
):]
53 if command
in self
.help_command
:
54 return self
.help_command
[command
]
58 def add_help_command(self
, command
):
59 cmd
= self
.commands
[command
]
61 if isinstance(cmd
, basestring
):
62 cmd
= self
.commands
[cmd
]
70 message
.append('@b%s%s: %s' % (self
.prefix
, command
, cmd_desc
))
73 msg
= 'Usage: @b%s%s@b' % (self
.prefix
, command
)
76 msg
+= ' ' + ' '.join(['[%s%s]' % (cmd_arg
[1], (' ' + cmd_arg
[0]) if ('action' not in cmd_arg
[3] or cmd_arg
[3]['action'] != 'store_true') else '') for cmd_arg
in cmd_args
])
79 argument_name
= cmd
[4]
81 argument_name
= 'argument'
83 if cmd_type
& ARG_YES
:
84 msg
+= ' %s' % argument_name
85 elif cmd_type
& ARG_OPT
:
86 msg
+= ' [%s]' % argument_name
93 for cmd_arg
in cmd_args
:
94 longest
= len(cmd_arg
[0]) if len(cmd_arg
[0]) > longest
else longest
96 for cmd_arg
in cmd_args
:
97 message
.append('@b--%-*s (%s)@b %s' % (longest
+ 1, cmd_arg
[0], cmd_arg
[1], cmd_arg
[2]))
99 self
.help_command
[command
] = message
102 def generate_help(self
):
105 self
.help.append('@bCommands@b (type @b%shelp command name@b for detailed information):' % self
.prefix
)
106 self
.help.append(' ')
112 for cmd
in self
.commands
:
113 if isinstance(self
.commands
[cmd
], basestring
):
114 orig
= self
.commands
[cmd
]
116 if orig
in alias_dict
:
117 alias_dict
[orig
].append(cmd
)
119 alias_dict
[orig
] = [cmd
]
121 if not cmd
in alias_dict
:
124 for key
in alias_dict
:
125 cur
= key
+ ('' if len(alias_dict
[key
]) == 0 else (' (' + ', '.join(alias_dict
[key
]) + ')'))
126 longest
= len(cur
) if len(cur
) > longest
else longest
127 commands
[cur
] = self
.commands
[key
][2]
129 for cmd
in sorted(commands
):
130 self
.help.append('@b%-*s@b %s' % (longest
+ 1, cmd
, commands
[cmd
]))
132 self
.help_command
= {}
134 for command
in self
.commands
:
135 self
.add_help_command(command
)