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 @@
""" Entry file for the Python todo.txt CLI. """
import getopt
import sys
def usage():
""" 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
subcommand was given and it fallbacks to the default subcommand.
"""
try:
values = sys.argv[p_start:]
except IndexError:
usage()
print """\
-c : Specify an alternative configuration file.
-d : Specify an alternative archive file (done.txt)
-h : This help text
-t : Specify an alternative todo file
-v : Print version and exit
"""
return values
exit(0)
def write(p_file, p_string):
"""
......@@ -54,6 +49,13 @@ def error(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
# First thing is to poke the configuration and check whether it's sane
......@@ -88,6 +90,38 @@ class CLIApplication(object):
def __init__(self):
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):
"""
Performs an archive action on the todolist.
......@@ -95,7 +129,7 @@ class CLIApplication(object):
This means that all completed tasks are moved to the archive file
(defaults to done.txt).
"""
archive_file = TodoFile.TodoFile(config().archive())
archive_file = TodoFile.TodoFile(self.archive_path)
archive = TodoList.TodoList(archive_file.read())
if archive:
......@@ -107,13 +141,15 @@ class CLIApplication(object):
def run(self):
""" Main entry function. """
todofile = TodoFile.TodoFile(config().todotxt())
args = self._process_flags()
todofile = TodoFile.TodoFile(self.path)
self.todolist = TodoList.TodoList(todofile.read())
try:
subcommand = sys.argv[1]
subcommand = args.pop(0)
except IndexError:
subcommand = config().default_command()
subcommand = self.config.default_command()
subcommand_map = {
'add': AddCommand,
......@@ -139,10 +175,8 @@ class CLIApplication(object):
'tag': TagCommand,
}
args = arguments()
if not subcommand in subcommand_map:
subcommand = config().default_command()
args = arguments(1)
subcommand = self.config.default_command()
command = subcommand_map[subcommand](args, self.todolist,
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