diff --git a/pubs/command_utils.py b/pubs/command_utils.py new file mode 100644 index 0000000..445f203 --- /dev/null +++ b/pubs/command_utils.py @@ -0,0 +1,20 @@ +"""Contains code that is reused over commands, like argument definition +or help messages. +""" + + +def add_doc_add_arguments(parser, move=True): + doc_add_group = parser.add_mutually_exclusive_group() + doc_add_group.add_argument( + '-L', '--link', action='store_const', dest='doc_add', const='link', + default=None, + help="don't copy document files, just create a link.") + doc_add_group.add_argument( + '-C', '--copy', action='store_const', dest='doc_add', const='copy', + default=None, + help="copy document (keep source).") + if move: + doc_add_group.add_argument( + '-M', '--move', action='store_const', dest='doc_add', const='move', + default=None, + help="move document (copy and remove source).") diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 9487d05..4510db7 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -12,6 +12,7 @@ from .. import apis from .. import pretty from .. import utils from .. import endecoder +from ..command_utils import add_doc_add_arguments from ..completion import CommaSeparatedTagsCompletion @@ -34,19 +35,7 @@ def parser(subparsers, conf): ).completer = CommaSeparatedTagsCompletion(conf) parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.', default=None, type=p3.u_maybe) - doc_add_group = parser.add_mutually_exclusive_group() - doc_add_group.add_argument( - '-L', '--link', action='store_const', dest='doc_add', const='link', - default=None, - help="don't copy document files, just create a link.") - doc_add_group.add_argument( - '-M', '--move', action='store_const', dest='doc_add', const='move', - default=None, - help="move document instead of of copying.") - doc_add_group.add_argument( - '-C', '--copy', action='store_const', dest='doc_add', const='copy', - default=None, - help="copy document instead of of move.") + add_doc_add_arguments(parser) return parser diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index fa6236e..8f11bc9 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -8,9 +8,9 @@ from .. import endecoder from .. import bibstruct from .. import color from ..paper import Paper - from ..uis import get_ui from ..content import system_path, read_text_file +from ..command_utils import add_doc_add_arguments _ABORT_USE_IGNORE_MSG = "Aborting import. Use --ignore-malformed to ignore." @@ -18,18 +18,22 @@ _IGNORING_MSG = " Ignoring it." def parser(subparsers, conf): - parser = subparsers.add_parser('import', - help='import paper(s) to the repository') - parser.add_argument('bibpath', - help='path to bibtex, bibtexml or bibyaml file (or directory)') - parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True, - help="don't copy document files, just create a link.") - parser.add_argument('keys', nargs='*', - help="one or several keys to import from the file") - parser.add_argument('-O', '--overwrite', action='store_true', default=False, - help="Overwrite keys already in the database") - parser.add_argument('-i', '--ignore-malformed', action='store_true', default=False, - help="Ignore malformed and unreadable files and entries") + parser = subparsers.add_parser( + 'import', + help='import paper(s) to the repository') + parser.add_argument( + 'bibpath', + help='path to bibtex, bibtexml or bibyaml file (or directory)') + parser.add_argument( + 'keys', nargs='*', + help="one or several keys to import from the file") + parser.add_argument( + '-O', '--overwrite', action='store_true', default=False, + help="Overwrite keys already in the database") + parser.add_argument( + '-i', '--ignore-malformed', action='store_true', default=False, + help="Ignore malformed and unreadable files and entries") + add_doc_add_arguments(parser, move=False) return parser @@ -90,9 +94,10 @@ def command(conf, args): ui = get_ui() bibpath = args.bibpath - copy = args.copy - if copy is None: - copy = conf['main']['doc_add'] in ('copy', 'move') + doc_add = args.doc_add + if doc_add is None: + doc_add = conf['main']['doc_add'] + copy = doc_add in ('copy', 'move') rp = repo.Repository(conf) # Extract papers from bib