Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving off optparse #4659

Open
pradyunsg opened this issue Aug 9, 2017 · 48 comments
Open

Moving off optparse #4659

pradyunsg opened this issue Aug 9, 2017 · 48 comments
Labels
C: cli Command line interface related things (optparse, option grouping etc) state: awaiting PR Feature discussed, PR is needed type: enhancement Improvements to functionality type: maintenance Related to Development and Maintenance Processes

Comments

@pradyunsg
Copy link
Member

@pypa/pip-committers Thoughts on switching over to using argparse or even click for option parsing?

@pradyunsg pradyunsg added type: enhancement Improvements to functionality type: maintenance Related to Development and Maintenance Processes state: needs discussion This needs some more discussion type: refactor Refactoring code labels Aug 9, 2017
@xavfernandez
Copy link
Member

Since optparse is deprecated it would make sense to switch to argparse.
But I think @dstufft was planning to switch to click.

@pradyunsg
Copy link
Member Author

+1 for click from me.

I'm pretty sure there'd be some edge case the switch would break but that's fine.

@thechief389
Copy link

thechief389 commented May 5, 2018

I think you would be better off with argparse as it's in the standard library and you don't have to add a new dependency to it. It also has the argcomplete library which is good for tab completion.

Also, read this and see what you think: http://xion.io/post/programming/python-dont-use-click.html

@pradyunsg
Copy link
Member Author

Summary of the blog post:

click has an hands-off approach and the author feels that approach isn't appropriate when the CLI is your user interface.


That's a fair argument, made in a fairly emotional tone IMO. (maybe that python-dev thread making me look at things this way)

I'm happy with both options and likely argparse is going to be easier to switch to anyways.

@pradyunsg pradyunsg added state: awaiting PR Feature discussed, PR is needed and removed type: refactor Refactoring code labels May 6, 2018
@RonnyPfannschmidt
Copy link
Contributor

it should be noted that argparse has a few pretty nasty "bydesign" bugs around automatically aliasing the prefix pf an argument to the full version,
its only opt out in 3.6* and there is no fix for older pythons

@thechief389
Copy link

But nevertheless, argparse is the best choice.

@pradyunsg
Copy link
Member Author

@RonnyPfannschmidt do you have any links? My Googling couldn't find anything... :/

@RonnyPfannschmidt
Copy link
Contributor

https://docs.python.org/3/library/argparse.html#allow-abbrev - sorry for quoting the python version wrong, it was added in 3.5

@pradyunsg pradyunsg added this to the Internal Cleansing milestone Aug 12, 2018
@pradyunsg pradyunsg added the C: cli Command line interface related things (optparse, option grouping etc) label Aug 21, 2018
@thechief389
Copy link

I'm working on moving to click. Is there anyone else doing it?

@pradyunsg
Copy link
Member Author

I've done a bit of work in the past but ran out of time; mostly was just trying to figure out what all is worth investigating, how things could be done and potential improvements before actually starting working on code.

I do have some notes I can share -- none of what's there in the notes is set in stone but might be a good place to start from. You don't need to respond to every hypothetical in it but those should be figured out as we do this. :)

Please feel free to discuss what you're thinking here.


Investigate:

Plan:

  • Rework the existing command setup to have the same "structure".
  • No decorators.
    • Call a method / list of Option in pip.commands? pip.cli.cmdoptions?
  • Do A/B testing: use an env var to have conditional for how they're parsed
    • need to edit tox to pass whatever env var

To be used parts of the API:

  • Command
  • Group
  • Option
  • Context? (see above)

Avoid direct use:

  • ASIDE: why aren't these marked more visibly / separated better? File an issue?
  • CommandCollection
  • Parameter
  • BaseCommand
  • MultiCommand

@pradyunsg
Copy link
Member Author

Hey @thechief389! Have you made any progress on this?

@thechief389
Copy link

I had trouble trying to vendor all the libraries and their dependencies for click and click-completion.

@techtonik
Copy link
Contributor

techtonik commented Jan 15, 2019

Another argument against click. After spending 15 minutes I haven't found the code to implement help in this manner:

  1. <command> -h
  2. <command> --help
  3. help <command>
  4. help <command> -h
  5. help <command> --help
  6. help

There 4. 5. 6. are equivalent to help -h or help --help.

@davidism
Copy link

Hello, I'm one of the Pallets (Click) core maintainers. I'm happy to answer questions you have, although I'd ask that you ping me here or in our Discord rather than opening up issues for questions in the repo.

I think we're already set up to be vendorable, and if not we can fix that. If you notice any bugs while trying to integrate Click with Pip, please let us know.

@davidism
Copy link

The basic implementation of a help name command, although it should be improved:

@cli.command()
@click.argument("name")
@click.pass_context
def help(ctx, name):
    command = cli.get_command(ctx, name)
    click.echo(command.get_help(ctx))

@pradyunsg
Copy link
Member Author

Thanks for pitching in here @davidsm! :)

I'll ping here if and when I work on this. :)

@pradyunsg pradyunsg removed the state: needs discussion This needs some more discussion label Jan 15, 2019
@cjerdonek
Copy link
Member

Because of the size of the code base, I think it's important to have an implementation plan that would let us make progress on this incrementally.

@pradyunsg
Copy link
Member Author

We can't really have half our parsing be optparse and the other half be click. I think you know that. ;) I think you mean what the plan is for separating concerns within pip's codebase about this to ease the actual "switch". My current thought process for that is:

  • modify pip's Commands to stop passing "options" to other internal objects (like to pip_version_check).
    • mypy annotations should help here, though I don't think they'll cover 100%.
  • create custom Option classes and adding code to convert them into concrete optparse variants would make life easier later on.
  • it should be not-too-difficult to create an optparse.Values-like object using click's higher-level classes to be passed to Commands

PS: it's past 1 am. Don't quote me on these. :)

@cjerdonek
Copy link
Member

We can't really have half our parsing be optparse and the other half be click.

That's not what I was saying.. I was saying I think it's important for us to have a way to do it that lets us make changes to the code gradually over time. There can be many possible approaches.

For example, maybe there is a way to add a translation / compatibility layer to have whatever we're migrating to (argparse, click, or whatever) accept optparse options. That would let us switch the options one at a time from optparse to the new format.

The point is to avoid a massive PR or massive amounts of code getting turned on at once. The approach should let us incrementally add new code that is getting used as we add it.

@thechief389
Copy link

Pipenv uses click and it works well.

@techtonik
Copy link
Contributor

techtonik commented Jan 21, 2019

Poetry looks better and uses cleo.

@pradyunsg
Copy link
Member Author

pradyunsg commented Jan 21, 2019

It's pretty simple to use click's underlying classes to make a CLI like pip's. The more fun part is figuring out how the transition for pip's optparse based code to have click options and making commands get Values-like objects, as @cjerdonek pointed out.

Click to reveal code-dump
import inspect
import functools

import click

from .commands import get_commands


def call_with_context(func):
    return func(click.get_current_context())


def create_click_command_from_our_command(our_command):
    # Convert the options; this bit has to be figured out.
    params = [
        click.Option(opt.param_decls, **opt.kwargs) for opt in our_command.options
    ]

    # Construct the click command
    cmd = click.Command(
        our_command.name,
        short_help=inspect.getdoc(our_command),
        callback=functools.partial(call_with_context, our_command.run),
        params=params,
    )

    return cmd


def main():
    cli = click.Group()
    for our_command in get_commands():
        cli.add_command(create_click_command_from_our_command(our_command))

    cli.main(prog_name="pip")

@techtonik
Copy link
Contributor

Click doesn't support custom formatting - pallets/click#561

@davidism
Copy link

davidism commented Feb 10, 2019

There are plenty of ways to override formatting now, adding the ability to override the full formatter class (the issue you linked) is an extension to what's already possible.

@thechief389
Copy link

thechief389 commented Feb 10, 2019 via email

@pradyunsg
Copy link
Member Author

pradyunsg commented Apr 15, 2020

Here's a rough draft of what I'd imagined we'd end up with in terms of how the commands' option declaration looks like. I'm not looking for feedback / inputs on this at this time. I'm noting it here in case someone else comes around to work on this and would like to know a rough target of what I want us to achieve.

...
from pip._internal.cli.base_command import Command
from pip._internal.cli.options import common_options, Choice, IndexOptionsMixin, Option
...


class ListCommand(Command, IndexOptionsMixin):
    """
    List installed packages, including editables.

    Packages are listed in a case-insensitive sorted order.
    """

    usage = """
      %prog [options]"""

    options = [
        Option(
            "-o", "--outdated",
            is_flag=True,
            help="List outdated packages",
        ),
        Option(
            "-u", "--uptodate",
            is_flag=True,
            help="List up-to-date packages",
        ),
        Option(
            "-e", "--editable",
            is_flag=True,
            help="List editable projects.",
        ),
        Option(
            "-l",
            "--local",
            is_flag=True,
            help=(
                "If in a virtualenv that has global access, do not list "
                "globally-installed packages."
            ),
        ),
        Option(
            "--user",
            is_flag=True,
            help="Only output packages installed in user-site."
        ),
        common_options.list_path,
        Option(
            "--pre",
            is_flag=True,
            help=(
                "Include pre-release and development versions. By default, "
                "pip only finds stable versions."
            ),
        ),
        Option(
            "--format",
            type=Choice(["columns", "freeze", "json"]),
            default="columns",
            dest="list_format",
            help=(
                "Select the output format among: columns (default), freeze, or json"
            ),
        ),
        Option(
            "--not-required",
            is_flag=True,
            help="List packages that are not dependencies of installed packages.",
        ),
        Option(
            "--include-editable/--exclude-editable",
            is_flag=True,
            help="Include editable package from output.",
        ),
    ]

    # No need for wrangling with `self.cmd_opts` in `__init__`
    # Rest of the command definition follows immediately.

@sinscary
Copy link
Contributor

sinscary commented Sep 4, 2020

@pradyunsg Is this still up for development?

@pradyunsg
Copy link
Member Author

@sinscary certainly!

@sinscary
Copy link
Contributor

sinscary commented Sep 4, 2020

@pradyunsg In that case I would like to give it a try.

@pradyunsg
Copy link
Member Author

Please do! In case you want to have a voice / video chat about this, free free to reach out to me over email. :)

@janluke
Copy link

janluke commented Aug 7, 2021

Shameless plug: in case you need additional features for Click, like argparse argument groups, you may consider Cloup. To my knowledge, it's the only package that allows to define option groups without using decorators with Click.

@pradyunsg
Copy link
Member Author

pradyunsg commented Feb 7, 2022

Well, here's a fun thing that's going to make this a bit more painful:

I ran pip wheel setuptools --build /tmp/fooooo (we removed the --build/--build-dir options in 21.3) and optparse has been a very helpful parser and completed that to --build-option. From a breakpoint at the start of WheelCommand.run:

(Pdb) options.build_options
['/tmp/fooooo']
(Pdb) sys.argv
['/Users/pgedam/Developer/totally-normal-nothing-to-see-here-project/.venv/bin/pip', 'wheel', 'setuptools', '--build', '/tmp/fooooo']

🤦🏽

@Shivansh-007
Copy link
Contributor

How about using sdispater/cleo @pradyunsg? It supports the class-based approach that we are wanting to have, along with some other pretty features like text colouring.

How poetry uses it for its CLI - https://github.com/python-poetry/poetry/blob/6485bc23d6497c7731e0f1a635f960b33f2ae99e/src/poetry/console/commands/export.py#L9-L38

@uranusjr
Copy link
Member

Actually from the above discussion argparse is by far the best choice. The only blocker against it has been removed now pip has dropped all Python <3.6. And since it’s stdlib, nothing can really beat it at this point (plus migrating from optparse to argparse might actually be the least work due to their similarities in interface design).

@Shivansh-007
Copy link
Contributor

For argparse, we would need to build some features from scratch as we have currently done for optparse, IMO command-line libraries like click and cleo are doing a much great job at the interface they are providing for building apps and have pretty good features.

Migrating to cleo won't be a big pain as they share a bit of similarity, the way we currently have them written is similar to cleo's style.

@uranusjr
Copy link
Member

I wouldn’t mind reviewing a POC if you feel it is easy enough to do. Note that we won’t be using much of the features apart from command line parsing (e.g. colors) in either case since those are already done with other mechanisms.

@notatallshaw
Copy link
Member

FWIW it seems that while optparse is "deprecated" it's clearly the opinion, in this discussion about an argparse bug, that optparse will never be removed and argparse development itself has largely slowed down in the face of design issues,

@slightlybelowzen
Copy link

slightlybelowzen commented Feb 9, 2024

I'd be happy to work on a small POC for this (using argparse) if there's still interest in moving off of optparse. We can decide where to go from there.

@pradyunsg
Copy link
Member Author

There is, and please feel welcome to do a PoC!

@slightlybelowzen
Copy link

slightlybelowzen commented Feb 9, 2024

Also I don't know if I'm missing something but in the docs it says, the cli/ subpackage is implemented using argparse which seems off?

* ``cli/`` *[subpackage containing helpers & additional code for managing the command line interface. Uses argparse from stdlib]*

Hopefully after this PR is merged, we won't need to update it😁!

@uranusjr
Copy link
Member

The doc entry is mostly likely a typo, it’s added much later than the actual implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: cli Command line interface related things (optparse, option grouping etc) state: awaiting PR Feature discussed, PR is needed type: enhancement Improvements to functionality type: maintenance Related to Development and Maintenance Processes
Projects
None yet
Development

No branches or pull requests