Re: [PATCH 1 of 3 v4] flags: allow specifying --no-boolean-flag on the command line (BC)

2016-09-17 Thread Yuya Nishihara
On Fri, 16 Sep 2016 11:15:01 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler 
> # Date 1473821877 14400
> #  Tue Sep 13 22:57:57 2016 -0400
> # Node ID cb57a762749f29065635d0480bf5c87c644ba0c5
> # Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110
> flags: allow specifying --no-boolean-flag on the command line (BC)

Queued this, thanks.

> +Test that boolean flags allow --flag=false specification to override 
> [defaults]

I made s/--flag=false/--no-flag/ in flight.
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH 1 of 3 v4] flags: allow specifying --no-boolean-flag on the command line (BC)

2016-09-16 Thread Augie Fackler
# HG changeset patch
# User Augie Fackler 
# Date 1473821877 14400
#  Tue Sep 13 22:57:57 2016 -0400
# Node ID cb57a762749f29065635d0480bf5c87c644ba0c5
# Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110
flags: allow specifying --no-boolean-flag on the command line (BC)

This makes it much easier to enable some anti-foot-shooting features
(like update --check) by default, because now all boolean flags can be
explicitly disabled on the command line without having to use HGPLAIN
or similar. Flags which don't deserve this treatment can be removed
from consideration by adding them to the nevernegate set in fancyopts.

This doesn't make it any easier to identify when a flag is set: opts
still always gets filled in, either with the user-specified flag value
or with the default from the flags list in the command
table. Improving that would probably clean things up a bit, but for
now if you want a boolean flag and care if it was explicitly false or
default false (or true, but nobody uses that functionality because
before now it was nonsense) you need to use None as your default
rather than True or False.

This doesn't (yet) update help output, because I'm not quite sure how
to do that cleanly.

diff --git a/mercurial/fancyopts.py b/mercurial/fancyopts.py
--- a/mercurial/fancyopts.py
+++ b/mercurial/fancyopts.py
@@ -12,6 +12,17 @@ import getopt
 from .i18n import _
 from . import error
 
+# Set of flags to not apply boolean negation logic on
+nevernegate = set([
+# avoid --no-noninteractive
+'noninteractive',
+# These two flags are special because they cause hg to do one
+# thing and then exit, and so aren't suitable for use in things
+# like aliases anyway.
+'help',
+'version',
+])
+
 def gnugetopt(args, options, longoptions):
 """Parse options mostly like getopt.gnu_getopt.
 
@@ -64,6 +75,8 @@ def fancyopts(args, options, state, gnu=
 shortlist = ''
 argmap = {}
 defmap = {}
+negations = {}
+alllong = set(o[1] for o in options)
 
 for option in options:
 if len(option) == 5:
@@ -91,6 +104,18 @@ def fancyopts(args, options, state, gnu=
 short += ':'
 if oname:
 oname += '='
+elif oname not in nevernegate:
+if oname.startswith('no-'):
+insert = oname[3:]
+else:
+insert = 'no-' + oname
+# backout (as a practical example) has both --commit and
+# --no-commit options, so we don't want to allow the
+# negations of those flags.
+if insert not in alllong:
+assert ('--' + oname) not in negations
+negations['--' + insert] = '--' + oname
+namelist.append(insert)
 if short:
 shortlist += short
 if name:
@@ -105,6 +130,11 @@ def fancyopts(args, options, state, gnu=
 
 # transfer result to state
 for opt, val in opts:
+boolval = True
+negation = negations.get(opt, False)
+if negation:
+opt = negation
+boolval = False
 name = argmap[opt]
 obj = defmap[name]
 t = type(obj)
@@ -121,7 +151,7 @@ def fancyopts(args, options, state, gnu=
 elif t is type([]):
 state[name].append(val)
 elif t is type(None) or t is type(False):
-state[name] = True
+state[name] = boolval
 
 # return unparsed args
 return args
diff --git a/tests/test-update-branches.t b/tests/test-update-branches.t
--- a/tests/test-update-branches.t
+++ b/tests/test-update-branches.t
@@ -379,3 +379,14 @@ Test experimental revset support
 
   $ hg log -r '_destupdate()'
   2:bd10386d478c 2 (no-eol)
+
+Test that boolean flags allow --flag=false specification to override [defaults]
+  $ cat >> $HGRCPATH < [defaults]
+  > update = --check
+  > EOF
+  $ hg co 2
+  abort: uncommitted changes
+  [255]
+  $ hg co --no-check 2
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel