Commit f85dfe5a authored by Bram Schoenmakers's avatar Bram Schoenmakers

Implement flags for CLI script.

-c, -d and -t are supported to specify alternative configuration files,
archive files or todo.txt files respectively.

The configuration file specified with -c has the highest priority.

The files specified with -d or -t are always used regardless of the
configuration.
parent f358c227
...@@ -16,26 +16,21 @@ ...@@ -16,26 +16,21 @@
""" Entry file for the Python todo.txt CLI. """ """ Entry file for the Python todo.txt CLI. """
import getopt
import sys import sys
def usage(): def usage():
""" Prints the usage of the todo.txt CLI """ """ Prints the usage of the todo.txt CLI """
exit(1)
def arguments(p_start=2):
"""
Retrieves all values from the argument list starting from the given
position.
This is a parameter, because argv has a different structure when no print """\
subcommand was given and it fallbacks to the default subcommand. -c : Specify an alternative configuration file.
""" -d : Specify an alternative archive file (done.txt)
try: -h : This help text
values = sys.argv[p_start:] -t : Specify an alternative todo file
except IndexError: -v : Print version and exit
usage() """
return values exit(0)
def write(p_file, p_string): def write(p_file, p_string):
""" """
...@@ -54,6 +49,13 @@ def error(p_string): ...@@ -54,6 +49,13 @@ def error(p_string):
write(sys.stderr, p_string) write(sys.stderr, p_string)
def version():
""" Print the current version and exit. """
from topydo.lib.Version import VERSION, LICENSE
print "topydo %s\n" % (VERSION)
print LICENSE
exit(0)
from topydo.lib.Config import config, ConfigError from topydo.lib.Config import config, ConfigError
# First thing is to poke the configuration and check whether it's sane # First thing is to poke the configuration and check whether it's sane
...@@ -88,6 +90,38 @@ class CLIApplication(object): ...@@ -88,6 +90,38 @@ class CLIApplication(object):
def __init__(self): def __init__(self):
self.todolist = TodoList.TodoList([]) self.todolist = TodoList.TodoList([])
self.config = config()
self.path = self.config.todotxt()
self.archive_path = self.config.archive()
def _process_flags(self):
try:
opts, args = getopt.getopt(sys.argv[1:], "c:d:ht:v")
except getopt.GetoptError as e:
error(str(e))
exit(1)
alt_path = None
alt_archive = None
for opt, value in opts:
if opt == "-c":
self.config = config(value)
elif opt == "-t":
alt_path = value
elif opt == "-d":
alt_archive = value
elif opt == "-v":
version()
else:
usage()
self.path = alt_path if alt_path else self.config.todotxt()
self.archive_path = alt_archive \
if alt_archive else self.config.archive()
return args
def archive(self): def archive(self):
""" """
Performs an archive action on the todolist. Performs an archive action on the todolist.
...@@ -95,7 +129,7 @@ class CLIApplication(object): ...@@ -95,7 +129,7 @@ class CLIApplication(object):
This means that all completed tasks are moved to the archive file This means that all completed tasks are moved to the archive file
(defaults to done.txt). (defaults to done.txt).
""" """
archive_file = TodoFile.TodoFile(config().archive()) archive_file = TodoFile.TodoFile(self.archive_path)
archive = TodoList.TodoList(archive_file.read()) archive = TodoList.TodoList(archive_file.read())
if archive: if archive:
...@@ -107,13 +141,15 @@ class CLIApplication(object): ...@@ -107,13 +141,15 @@ class CLIApplication(object):
def run(self): def run(self):
""" Main entry function. """ """ Main entry function. """
todofile = TodoFile.TodoFile(config().todotxt()) args = self._process_flags()
todofile = TodoFile.TodoFile(self.path)
self.todolist = TodoList.TodoList(todofile.read()) self.todolist = TodoList.TodoList(todofile.read())
try: try:
subcommand = sys.argv[1] subcommand = args.pop(0)
except IndexError: except IndexError:
subcommand = config().default_command() subcommand = self.config.default_command()
subcommand_map = { subcommand_map = {
'add': AddCommand, 'add': AddCommand,
...@@ -139,10 +175,8 @@ class CLIApplication(object): ...@@ -139,10 +175,8 @@ class CLIApplication(object):
'tag': TagCommand, 'tag': TagCommand,
} }
args = arguments()
if not subcommand in subcommand_map: if not subcommand in subcommand_map:
subcommand = config().default_command() subcommand = self.config.default_command()
args = arguments(1)
command = subcommand_map[subcommand](args, self.todolist, command = subcommand_map[subcommand](args, self.todolist,
lambda o: write(sys.stdout, o), lambda o: write(sys.stdout, o),
......
""" Version of Topydo. """
VERSION = 0.1
LICENSE = """Copyright (C) 2014 Bram Schoenmakers
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."""
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