Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
code-webis-cmd
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
10
Issues
10
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
code-generic
code-webis-cmd
Commits
cd1bda40
Commit
cd1bda40
authored
Aug 14, 2015
by
Steve Goering
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add aliases and command help functions
parent
9a50ed75
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
8 deletions
+77
-8
config.json
config.json
+10
-1
libs/lib.py
libs/lib.py
+31
-1
webis.py
webis.py
+36
-6
No files found.
config.json
View file @
cd1bda40
...
...
@@ -5,6 +5,15 @@
"update"
:
"core/update.sh"
,
"version"
:
"core/version.sh"
,
"commands_help"
:
{
"core"
:
"Core functions of webis command."
,
"core"
:
"Core functions of webis command."
,
"test"
:
"A few unimportant test scripts for developing purpose."
,
"betamng"
:
"Low level scripts for managing betaweb."
,
"betaweb"
:
"High level scripts for betaweb, e.g. hadoop management."
},
"commands_aliases"
:
{
"core"
:
[
"co"
],
"betamng"
:
[
"bm"
],
"betaweb"
:
[
"bw"
]
}
}
\ No newline at end of file
libs/lib.py
View file @
cd1bda40
...
...
@@ -6,9 +6,39 @@
contact: stg7@gmx.de
2015
"""
import
re
import
os
import
argparse
class
AliasedSubParsersAction
(
argparse
.
_SubParsersAction
):
""" Based on : https://gist.github.com/sampsyo/471779 """
class
_AliasedPseudoAction
(
argparse
.
Action
):
def
__init__
(
self
,
name
,
aliases
,
help
):
dest
=
name
if
aliases
:
dest
+=
' (%s)'
%
','
.
join
(
aliases
)
sup
=
super
(
AliasedSubParsersAction
.
_AliasedPseudoAction
,
self
)
sup
.
__init__
(
option_strings
=
[],
dest
=
dest
,
help
=
help
)
def
add_parser
(
self
,
name
,
**
kwargs
):
if
'aliases'
in
kwargs
:
aliases
=
kwargs
[
'aliases'
]
del
kwargs
[
'aliases'
]
else
:
aliases
=
[]
parser
=
super
(
AliasedSubParsersAction
,
self
).
add_parser
(
name
,
**
kwargs
)
# Make the aliases work.
for
alias
in
aliases
:
self
.
_name_parser_map
[
alias
]
=
parser
# Make the help text reflect them, first removing old help entry.
if
'help'
in
kwargs
:
help
=
kwargs
.
pop
(
'help'
)
self
.
_choices_actions
.
pop
()
pseudo_action
=
self
.
_AliasedPseudoAction
(
name
,
aliases
,
help
)
self
.
_choices_actions
.
append
(
pseudo_action
)
return
parser
if
__name__
==
"__main__"
:
print
(
"
\033
[91m[ERROR]
\033
[0m lib is not a standalone module"
)
...
...
webis.py
View file @
cd1bda40
...
...
@@ -18,6 +18,18 @@ from log import *
from
system
import
*
def
check_commands_alias_definition
(
config
):
"""Checks if the command alias definition has no collisions."""
aliashisto
=
{}
for
command
in
config
[
"commands_aliases"
]:
for
alias
in
config
[
"commands_aliases"
][
command
]:
aliashisto
[
alias
]
=
aliashisto
.
get
(
alias
,
0
)
+
1
if
len
(
config
[
"commands_aliases"
].
keys
())
!=
len
(
aliashisto
.
keys
()):
lError
(
"Commands alias definition has collisions, please fix it."
)
sys
.
exit
(
1
)
def
load_config
():
"""Returns the configuration, based on the JSON configuration file."""
try
:
...
...
@@ -86,22 +98,36 @@ def run_subcommand(subcommand, subcommandpath, params=[]):
lError
(
"An error occurred while executing "
+
cmd
)
return
return_value
def
translate_alias_to_command
(
config
,
alias
):
"""Transforms a given alias to the unique command."""
for
command
in
config
[
"commands_aliases"
]:
if
alias
in
config
[
"commands_aliases"
][
cmd
]:
return
command
def
main
(
args
):
config
=
load_config
()
check_commands_alias_definition
(
config
)
commands
=
get_commands
(
config
)
parser
=
argparse
.
ArgumentParser
()
parser
.
register
(
'action'
,
'parsers'
,
AliasedSubParsersAction
)
subparsers
=
{}
allcommands
=
{}
commandparsers
=
parser
.
add_subparsers
(
metavar
=
'<command>'
,
dest
=
'<command>'
,
help
=
'Available commands:'
)
for
command
in
commands
:
# TODO: It is unclear where to obtain the help messages for commands.
# TODO: It is conceivable to introduce aliases for each command, but it
# is unclear where to obtain them. Simply using a prefix does not
# work for betamng and betaweb, since they share a long prefix.
help_msg
=
config
[
"commands_help"
].
get
(
command
,
colorred
(
"Please add a short description of {} "
"in config.json[commands_help]."
.
format
(
command
)))
commandparser
=
commandparsers
.
add_parser
(
command
,
help
=
'TODO: Help message for commands.'
)
command
,
aliases
=
config
[
"commands_aliases"
].
get
(
command
,
[]),
help
=
help_msg
)
subparsers
[
command
]
=
commandparser
subcommandparsers
=
commandparser
.
add_subparsers
(
metavar
=
'<subcommand>'
,
dest
=
'<subcommand>'
,
...
...
@@ -124,7 +150,11 @@ def main(args):
parameter
=
parseresults
[
1
]
if
len
(
argsdict
)
==
2
and
argsdict
[
'<subcommand>'
]
is
None
:
subparsers
[
argsdict
[
'<command>'
]].
print_help
()
command
=
argsdict
[
'<command>'
]
if
command
not
in
subparsers
:
command
=
translate_alias_to_command
(
command
)
subparsers
[
command
].
print_help
()
return
0
return
run_subcommand
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment