4e4961e60c6e4bb6a3bd30bc70576f00ea519930

Author
Yuval <yvw.bor@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(aliases): add basic argument parsing (#10710)

Diff

 1diff --git a/plugins/aliases/README.md b/plugins/aliases/README.md
 2index bfb6ab8c47b0f233600d853078836e48ba10094d..66fd84584745ccb57e6f5cb51eb4d244246646cb 100644
 3--- a/plugins/aliases/README.md
 4+++ b/plugins/aliases/README.md
 5@@ -17,6 +17,12 @@ Requirements: Python needs to be installed.
 6 
 7 - `acs`: show all aliases by group.
 8 
 9+- `acs -h/--help`: print help mesage.
10+
11 - `acs <keyword>`: filter aliases by `<keyword>` and highlight.
12 
13+- `acs -g <group>/--group <group`: show only aliases for group `<group>`. Multiple uses of the flag show all groups,
14+
15+- `acs --groups-only`: show only group names
16+
17   ![screenshot](https://cloud.githubusercontent.com/assets/3602957/11581913/cb54fb8a-9a82-11e5-846b-5a67f67ad9ad.png)
18diff --git a/plugins/aliases/cheatsheet.py b/plugins/aliases/cheatsheet.py
19index a7e3968b0e2e3cdef90ec47f285015c748db11f8..3362a6ab66fabc1c6b39aee8d3a6d35da01ac3f2 100644
20--- a/plugins/aliases/cheatsheet.py
21+++ b/plugins/aliases/cheatsheet.py
22@@ -2,6 +2,7 @@
23 import sys
24 import itertools
25 import termcolor
26+import argparse
27 
28 def parse(line):
29     left = line[0:line.find('=')].strip()
30@@ -26,7 +27,7 @@ def cheatsheet(lines):
31         target_aliases.extend(group_list)
32     return cheatsheet
33 
34-def pretty_print_group(key, aliases, highlight=None):
35+def pretty_print_group(key, aliases, highlight=None, only_groupname=False):
36     if len(aliases) == 0:
37         return
38     group_hl_formatter = lambda g, hl: termcolor.colored(hl, 'yellow').join([termcolor.colored(part, 'red') for part in ('[%s]' % g).split(hl)])
39@@ -35,21 +36,33 @@ def pretty_print_group(key, aliases, highlight=None):
40     alias_formatter = lambda alias: termcolor.colored('\t%s = %s' % alias[0:2], 'green')
41     if highlight and len(highlight)>0:
42         print (group_hl_formatter(key, highlight))
43-        print ('\n'.join([alias_hl_formatter(alias, highlight) for alias in aliases]))
44+        if not only_groupname:
45+            print ('\n'.join([alias_hl_formatter(alias, highlight) for alias in aliases]))
46     else:
47         print (group_formatter(key))
48-        print ('\n'.join([alias_formatter(alias) for alias in aliases]))
49+        if not only_groupname:
50+            print ('\n'.join([alias_formatter(alias) for alias in aliases]))
51     print ('')
52 
53-def pretty_print(cheatsheet, wfilter):
54+def pretty_print(cheatsheet, wfilter, group_list=None, groups_only=False):
55     sorted_key = sorted(cheatsheet.keys())
56     for key in sorted_key:
57+        if group_list and key not in group_list:
58+            continue
59         aliases = cheatsheet.get(key)
60         if not wfilter:
61-            pretty_print_group(key, aliases, wfilter)
62+            pretty_print_group(key, aliases, wfilter, groups_only)
63         else:
64             pretty_print_group(key, [ alias for alias in aliases if alias[0].find(wfilter)>-1 or alias[1].find(wfilter)>-1], wfilter)
65 
66 if __name__ == '__main__':
67+    parser = argparse.ArgumentParser(description="Pretty print aliases.")
68+    parser.add_argument('filter', nargs="*", help="search aliases matching string")
69+    parser.add_argument('-g', '--group', dest="group_list", action='append', help="only print aliases in given groups")
70+    parser.add_argument('--groups', dest='groups_only', action='store_true', help="only print alias groups")
71+    args = parser.parse_args()
72+
73     lines = sys.stdin.readlines()
74-    pretty_print(cheatsheet(lines), sys.argv[1] if len(sys.argv)>1 else None)
75+    group_list = args.group_list or None
76+    wfilter = " ".join(args.filter) or None
77+    pretty_print(cheatsheet(lines), wfilter, group_list, args.groups_only)