From eabca1fcdc7cd932049608cbab634dd0ed63542e Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 21 Nov 2011 22:42:08 +0100 Subject: Add class to simplify git argument handling --- gbp/git/args.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 gbp/git/args.py (limited to 'gbp/git/args.py') diff --git a/gbp/git/args.py b/gbp/git/args.py new file mode 100644 index 00000000..ff9077ea --- /dev/null +++ b/gbp/git/args.py @@ -0,0 +1,91 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2011 Guido Guenther +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +class GitArgs(object): + """ + Handle arguments to git commands + + >>> GitArgs('-h', '--no-foo').args + ['-h', '--no-foo'] + >>> GitArgs().add('--more-foo', '--less-bar').args + ['--more-foo', '--less-bar'] + >>> GitArgs().add_cond(1 > 2, '--opt', '--no-opt').args + ['--no-opt'] + >>> GitArgs().add_true(True, '--true').args + ['--true'] + >>> GitArgs().add_false(True, '--true').args + [] + >>> GitArgs().add_false(False, '--false').args + ['--false'] + """ + + def __init__(self, *args): + self._args = list(args) + + @property + def args(self): + return self._args + + def add(self, *args): + """ + Add arguments to argument list + """ + self._args += list(args) + return self + + def add_true(self, condition, *args): + """ + Add I{args} if I{condition} is C{True} + + @param condition: the condition to test + @type condition: C{bool} + @param args: arguments to add + """ + if condition: + self._args += list(args) + return self + + def add_false(self, condition, *args): + """ + Add I{args} if I{condition} is C{False} + + @param condition: the condition to test + @type condition: C{bool} + @param args: arguments to add + """ + self.add_true(not condition, *args) + return self + + def add_cond(self, condition, opt, noopt=[]): + """ + Add option I{opt} to I{alist} if I{condition} is C{True} + else add I{noopt}. + + @param condition: condition + @type condition: C{bool} + @param opt: option to add if I{condition} is C{True} + @type opt: C{str} or C{list} + @param noopt: option to add if I{condition} is C{False} + @type noopt: C{str} or C{list} + """ + if isinstance(opt, basestring): + opt = [ opt ] + if isinstance(noopt, basestring): + noopt = [ noopt ] + self._args += opt if condition else noopt + return self + -- cgit v1.2.3