Patchwork [1,of,8,v2] flags: allow specifying --no-boolean-flag on the command line (BC)

login
register
mail settings
Submitter Augie Fackler
Date Aug. 30, 2016, 8:16 p.m.
Message ID <4f76e8b136581d750cf9.1472588174@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/16487/
State Changes Requested
Headers show

Comments

Augie Fackler - Aug. 30, 2016, 8:16 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1472586362 14400
#      Tue Aug 30 15:46:02 2016 -0400
# Node ID 4f76e8b136581d750cf98395ea8034dbb9666b96
# Parent  12f8bef59bfa2739d0c5d8425ab494fd2fe38a81
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.

This doesn't (yet) update help output, because I'm not quite sure how
to do that cleanly.
Yuya Nishihara - Sept. 1, 2016, 3:27 p.m.
On Tue, 30 Aug 2016 16:16:14 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1472586362 14400
> #      Tue Aug 30 15:46:02 2016 -0400
> # Node ID 4f76e8b136581d750cf98395ea8034dbb9666b96
> # Parent  12f8bef59bfa2739d0c5d8425ab494fd2fe38a81
> 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.
> 
> 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
> @@ -64,6 +64,7 @@ def fancyopts(args, options, state, gnu=
>      shortlist = ''
>      argmap = {}
>      defmap = {}
> +    notnegated = set()
>  
>      for option in options:
>          if len(option) == 5:
> @@ -87,10 +88,18 @@ def fancyopts(args, options, state, gnu=
>  
>          # does it take a parameter?
>          if not (default is None or default is True or default is False):
> +            if oname.startswith('no-'):
> +                notnegated.add(oname)
> +            # Note slight weirdness: this means flags like --no-backup
> +            # for revert can be negated with --no-no-backup. This
> +            # seems fine.
> +            namelist.append('no-' + oname)

--no-takesparam is allowed, which seems wrong and fixed by the patch 6.

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

s/--flag=false/--no-flag/

> +  $ cat >> $HGRCPATH <<EOF
> +  > [defaults]
> +  > update = --check
> +  > EOF
> +  $ hg co 2
> +  abort: uncommitted changes
> +  [255]
> +  $ hg co --no-check 2

Patch

diff --git a/mercurial/fancyopts.py b/mercurial/fancyopts.py
--- a/mercurial/fancyopts.py
+++ b/mercurial/fancyopts.py
@@ -64,6 +64,7 @@  def fancyopts(args, options, state, gnu=
     shortlist = ''
     argmap = {}
     defmap = {}
+    notnegated = set()
 
     for option in options:
         if len(option) == 5:
@@ -87,10 +88,18 @@  def fancyopts(args, options, state, gnu=
 
         # does it take a parameter?
         if not (default is None or default is True or default is False):
+            if oname.startswith('no-'):
+                notnegated.add(oname)
+            # Note slight weirdness: this means flags like --no-backup
+            # for revert can be negated with --no-no-backup. This
+            # seems fine.
+            namelist.append('no-' + oname)
             if short:
                 short += ':'
             if oname:
                 oname += '='
+        else:
+            namelist.append('no-' + oname)
         if short:
             shortlist += short
         if name:
@@ -105,6 +114,10 @@  def fancyopts(args, options, state, gnu=
 
     # transfer result to state
     for opt, val in opts:
+        negated = False
+        if opt.startswith('--no-') and opt[2:] not in notnegated:
+            opt = '--' + opt[5:]
+            negated = True
         name = argmap[opt]
         obj = defmap[name]
         t = type(obj)
@@ -121,7 +134,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] = False if negated else True
 
     # 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 <<EOF
+  > [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