[PATCH] install.py: ignore -Z / --context

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] install.py: ignore -Z / --context

Mike Gilbert-2
The --context option accepts an optional argument, but only if it is
passed via --context=arg. The argparse module does not deal with this
properly.

To work around this, have argparse ignore this option, and filter out
any remaining arguments that start with a hyphen.

Bug: https://bugs.gentoo.org/699548
Signed-off-by: Mike Gilbert <[hidden email]>
---
 bin/install.py | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/bin/install.py b/bin/install.py
index d3789ed96..e56475ff1 100755
--- a/bin/install.py
+++ b/bin/install.py
@@ -111,12 +111,6 @@ def parse_args(args):
  action="store_true",
  dest="no_target_directory"
  )
- parser.add_argument(
- "--context",
- "-Z",
- action="store",
- dest="context"
- )
  parser.add_argument(
  "--verbose",
  "-v",
@@ -147,7 +141,7 @@ def parse_args(args):
 
  opts  = parsed_args[0]
  files = parsed_args[1]
- files = [f for f in files if f != "--"] # filter out "--"
+ files = [f for f in files if not f.startswith("-")] # filter unknown options
 
  return (opts, files)
 
--
2.24.0


Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] install.py: ignore -Z / --context

Zac Medico-2
On 11/7/19 10:22 AM, Mike Gilbert wrote:

> The --context option accepts an optional argument, but only if it is
> passed via --context=arg. The argparse module does not deal with this
> properly.
>
> To work around this, have argparse ignore this option, and filter out
> any remaining arguments that start with a hyphen.
>
> Bug: https://bugs.gentoo.org/699548
> Signed-off-by: Mike Gilbert <[hidden email]>
> ---
>  bin/install.py | 8 +-------
>  1 file changed, 1 insertion(+), 7 deletions(-)
>
> diff --git a/bin/install.py b/bin/install.py
> index d3789ed96..e56475ff1 100755
> --- a/bin/install.py
> +++ b/bin/install.py
> @@ -111,12 +111,6 @@ def parse_args(args):
>   action="store_true",
>   dest="no_target_directory"
>   )
> - parser.add_argument(
> - "--context",
> - "-Z",
> - action="store",
> - dest="context"
> - )
>   parser.add_argument(
>   "--verbose",
>   "-v",
> @@ -147,7 +141,7 @@ def parse_args(args):
>  
>   opts  = parsed_args[0]
>   files = parsed_args[1]
> - files = [f for f in files if f != "--"] # filter out "--"
> + files = [f for f in files if not f.startswith("-")] # filter unknown options
>  
>   return (opts, files)
>  
>
Technically, shouldn't we pass through any "files" that start with "-"
and come after the "--" separator?
--
Thanks,
Zac


signature.asc (1000 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] install.py: ignore -Z / --context

Mike Gilbert-2
On Thu, Nov 7, 2019 at 3:31 PM Zac Medico <[hidden email]> wrote:

>
> On 11/7/19 10:22 AM, Mike Gilbert wrote:
> > The --context option accepts an optional argument, but only if it is
> > passed via --context=arg. The argparse module does not deal with this
> > properly.
> >
> > To work around this, have argparse ignore this option, and filter out
> > any remaining arguments that start with a hyphen.
> >
> > Bug: https://bugs.gentoo.org/699548
> > Signed-off-by: Mike Gilbert <[hidden email]>
> > ---
> >  bin/install.py | 8 +-------
> >  1 file changed, 1 insertion(+), 7 deletions(-)
> >
> > diff --git a/bin/install.py b/bin/install.py
> > index d3789ed96..e56475ff1 100755
> > --- a/bin/install.py
> > +++ b/bin/install.py
> > @@ -111,12 +111,6 @@ def parse_args(args):
> >               action="store_true",
> >               dest="no_target_directory"
> >       )
> > -     parser.add_argument(
> > -             "--context",
> > -             "-Z",
> > -             action="store",
> > -             dest="context"
> > -     )
> >       parser.add_argument(
> >               "--verbose",
> >               "-v",
> > @@ -147,7 +141,7 @@ def parse_args(args):
> >
> >       opts  = parsed_args[0]
> >       files = parsed_args[1]
> > -     files = [f for f in files if f != "--"] # filter out "--"
> > +     files = [f for f in files if not f.startswith("-")] # filter unknown options
> >
> >       return (opts, files)
> >
> >
>
> Technically, shouldn't we pass through any "files" that start with "-"
> and come after the "--" separator?

Ah, yes. I will revise it.

Reply | Threaded
Open this post in threaded view
|

[PATCH v2] install.py: ignore -Z / --context

Mike Gilbert-2
In reply to this post by Mike Gilbert-2
The --context option accepts an optional argument, but only if it is
passed via --context=arg. The argparse module does not deal with this
properly.

To work around this, have argparse ignore this option, and filter out
any remaining arguments that start with a hyphen and do not occur after
a "--" delimiter.

Bug: https://bugs.gentoo.org/699548
Signed-off-by: Mike Gilbert <[hidden email]>
---

If there is a cleaner way to do this, suggestions are welcome.

diff --git a/bin/install.py b/bin/install.py
index d3789ed96..495534d33 100755
--- a/bin/install.py
+++ b/bin/install.py
@@ -111,12 +111,6 @@ def parse_args(args):
  action="store_true",
  dest="no_target_directory"
  )
- parser.add_argument(
- "--context",
- "-Z",
- action="store",
- dest="context"
- )
  parser.add_argument(
  "--verbose",
  "-v",
@@ -143,11 +137,21 @@ def parse_args(args):
  # for known options in order for argparse to correctly
  # separate option arguments from file arguments in all
  # cases (it also allows for optparse compatibility).
- parsed_args = parser.parse_known_args()
+ (opts, args) = parser.parse_known_args(args)
+
+ files = []
+ i = 0
+ while i < len(args):
+ if args[i] == "--":
+ i += 1
+ break
+ if not args[i].startswith("-"):
+ files.append(args[i])
+ i += 1
 
- opts  = parsed_args[0]
- files = parsed_args[1]
- files = [f for f in files if f != "--"] # filter out "--"
+ while i < len(args):
+ files.append(args[i])
+ i += 1
 
  return (opts, files)
 
--
2.24.0


Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2] install.py: ignore -Z / --context

Zac Medico-2
On 11/7/19 1:18 PM, Mike Gilbert wrote:

> The --context option accepts an optional argument, but only if it is
> passed via --context=arg. The argparse module does not deal with this
> properly.
>
> To work around this, have argparse ignore this option, and filter out
> any remaining arguments that start with a hyphen and do not occur after
> a "--" delimiter.
>
> Bug: https://bugs.gentoo.org/699548
> Signed-off-by: Mike Gilbert <[hidden email]>
> ---
>
> If there is a cleaner way to do this, suggestions are welcome.
>
> diff --git a/bin/install.py b/bin/install.py
> index d3789ed96..495534d33 100755
> --- a/bin/install.py
> +++ b/bin/install.py
> @@ -111,12 +111,6 @@ def parse_args(args):
>   action="store_true",
>   dest="no_target_directory"
>   )
> - parser.add_argument(
> - "--context",
> - "-Z",
> - action="store",
> - dest="context"
> - )
>   parser.add_argument(
>   "--verbose",
>   "-v",
> @@ -143,11 +137,21 @@ def parse_args(args):
>   # for known options in order for argparse to correctly
>   # separate option arguments from file arguments in all
>   # cases (it also allows for optparse compatibility).
> - parsed_args = parser.parse_known_args()
> + (opts, args) = parser.parse_known_args(args)
> +
> + files = []
> + i = 0
> + while i < len(args):
> + if args[i] == "--":
> + i += 1
> + break
> + if not args[i].startswith("-"):
> + files.append(args[i])
> + i += 1
>  
> - opts  = parsed_args[0]
> - files = parsed_args[1]
> - files = [f for f in files if f != "--"] # filter out "--"
> + while i < len(args):
> + files.append(args[i])
> + i += 1
>  
>   return (opts, files)
>  
>

Looks good. Please merge.
--
Thanks,
Zac