Skip to content

Command Line Interface Fails #797

Closed
Closed
@gledi

Description

I wanted to test aiohttp and I started by writing a file named example.py as shown below:

from aiohttp import web

async def hi(request):
    name = request.match_info.get('name', 'Anonymous')
    text = 'Hi ' + name
    return web.Response(body=text.encode('utf-8'))

def startapp(args):
    app = web.Application()
    app.router.add_route('GET', '/{name}', hi)
    return app

In the docs I saw the section about the CLI and decide to try that. The example shown in the docs is written as:

$ python -m aiohttp.web -H localhost -P 8080 package.module.init_func

However it wasn't working and I took a look at the code and it turned out that it was using a : to split the module from the callable so I modified my command as follows:

(aiohttptest)[gledi@testvm aiotest]$ python -d -m aiohttp.web -H localhost -P 8080 example:startapp
usage: aiohttp.web [-h] [-H HOSTNAME] [-P PORT] entry-func
aiohttp.web: error: 'entry-func' not in 'module:function' syntax

As entry-func actually was in the module:function syntax I looked at the code and it turned out that you were passing sys.argv to main:

if __name__ == "__main__":
    main(sys.argv)

and then later on in main after creating a parser you were calling it as such:

args, extra_args = arg_parser.parse_known_args(argv)

However that includes as the first parameter the name of the module (.... aiohttp/web.py) and thus it failed to parse it properly. I changed it as follows and it worked for me:

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

...

if __name__ == '__main__':
    sys.exit(main()) # or simply just: main()

Also we might need to change the docs so that they use : to separate the module from the callable.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions