Commit 56967191 authored by Jacek Sowiński's avatar Jacek Sowiński

Create placeholder for args in aliases

With this change there is now possible to forward args supplied on CLI
to desired placed in real command.

Example config:

[aliases]
star = tag {} star 1
unstar = tag {} star

Example usage:

`topydo star foo` will resolve to: `topydo tag foo star 1`
`topydo unstar foo` will resolve to: `topydo tag foo star`
parent c0d74920
...@@ -3,3 +3,4 @@ foo = rm -f test ...@@ -3,3 +3,4 @@ foo = rm -f test
baz = FooBar baz = FooBar
format = ls -F "|I| x c d {(}p{)} s k" -n 25 format = ls -F "|I| x c d {(}p{)} s k" -n 25
smile = ls smile = ls
star = tag {} star 1
...@@ -22,6 +22,7 @@ from topydo.commands.AddCommand import AddCommand ...@@ -22,6 +22,7 @@ from topydo.commands.AddCommand import AddCommand
from topydo.commands.DeleteCommand import DeleteCommand from topydo.commands.DeleteCommand import DeleteCommand
from topydo.commands.ListCommand import ListCommand from topydo.commands.ListCommand import ListCommand
from topydo.commands.ListProjectCommand import ListProjectCommand from topydo.commands.ListProjectCommand import ListProjectCommand
from topydo.commands.TagCommand import TagCommand
from topydo.lib.Config import config from topydo.lib.Config import config
class GetSubcommandTest(TopydoTest): class GetSubcommandTest(TopydoTest):
...@@ -60,6 +61,14 @@ class GetSubcommandTest(TopydoTest): ...@@ -60,6 +61,14 @@ class GetSubcommandTest(TopydoTest):
self.assertTrue(issubclass(real_cmd, ListCommand)) self.assertTrue(issubclass(real_cmd, ListCommand))
self.assertEqual(final_args, [u"\u263b"]) self.assertEqual(final_args, [u"\u263b"])
def test_alias04(self):
config("test/data/aliases.conf")
args = ["star", "foo"]
real_cmd, final_args = get_subcommand(args)
self.assertTrue(issubclass(real_cmd, TagCommand))
self.assertEqual(final_args, ["foo", "star", "1"])
def test_default_cmd01(self): def test_default_cmd01(self):
args = ["bar"] args = ["bar"]
real_cmd, final_args = get_subcommand(args) real_cmd, final_args = get_subcommand(args)
......
...@@ -53,6 +53,8 @@ append_parent_contexts = 0 ...@@ -53,6 +53,8 @@ append_parent_contexts = 0
;showall = ls -x ;showall = ls -x
;next = ls -n 1 ;next = ls -n 1
;top = ls -F '%I %x %P %S %k %{(}H{)}' -N ;top = ls -F '%I %x %P %S %k %{(}H{)}' -N
;star = tag {} star 1
;unstar = tag {} star
;lsproj = lsprj ;lsproj = lsprj
;listprj = lsprj ;listprj = lsprj
;listproj = lsprj ;listproj = lsprj
......
...@@ -71,6 +71,18 @@ def get_subcommand(p_args): ...@@ -71,6 +71,18 @@ def get_subcommand(p_args):
__import__(modulename, globals(), locals(), [classname], 0) __import__(modulename, globals(), locals(), [classname], 0)
return getattr(sys.modules[modulename], classname) return getattr(sys.modules[modulename], classname)
def join_args(p_cli_args, p_alias_args):
"""
Returns properly joined args from alias definition and from user input.
"""
if '{}' in p_alias_args:
pos = p_alias_args.index('{}')
args = p_alias_args[:pos] + p_cli_args + p_alias_args[pos+1:]
else:
args = p_alias_args + p_cli_args
return args
def resolve_alias(p_alias, p_args): def resolve_alias(p_alias, p_args):
""" """
Resolves a subcommand alias and returns a tuple (Command, args). Resolves a subcommand alias and returns a tuple (Command, args).
...@@ -81,7 +93,7 @@ def get_subcommand(p_args): ...@@ -81,7 +93,7 @@ def get_subcommand(p_args):
real_subcommand, alias_args = alias_map[p_alias] real_subcommand, alias_args = alias_map[p_alias]
try: try:
result = import_subcommand(real_subcommand) result = import_subcommand(real_subcommand)
args = alias_args + p_args args = join_args(p_args, alias_args)
return (result, args) return (result, args)
except KeyError: except KeyError:
return get_subcommand(['help']) return get_subcommand(['help'])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment