
from aiohttp.web import Application, Response


def hello_world(req):
    return Response(text="Hello World")


def init(args):
    app = Application()
    app.router.add_route('GET', '/', hello_world)

    return app


from aiohttp.web import Application, Response
from argparse import ArgumentParser


def display_message(req):
    args = req.app["args"]
    text = "\n".join([args.message] * args.repeat)
    return Response(text=text)


def init(args):
    arg_parser = ArgumentParser(
        prog="aiohttp.web ...", description="Application CLI", add_help=False
    )

    # Positional argument
    arg_parser.add_argument(
        "message",
        help="message to print"
    )

    # Optional argument
    arg_parser.add_argument(
        "--repeat",
        help="number of times to repeat message", type=int, default="1"
    )

    # Avoid conflict with -h from `aiohttp.web` CLI parser
    arg_parser.add_argument(
        "--app-help",
        help="show this message and exit", action="help"
    )

    parsed_args = arg_parser.parse_args(args)

    app = Application()
    app["args"] = parsed_args
    app.router.add_route('GET', '/', display_message)

    return app

Update CLI example to use nested argparse
Update CLI example to use nested argparse

  
  from aiohttp.web import Application, Response
+ from argparse import ArgumentParser
  
  
- def hello_world(req):
+ def display_message(req):
+     args = req.app["args"]
+     text = "\n".join([args.message] * args.repeat)
-     return Response(text="Hello World")
+     return Response(text=text)
  
  
  def init(args):
+     arg_parser = ArgumentParser(
+         prog="aiohttp.web ...", description="Application CLI", add_help=False
+     )
+ 
+     # Positional argument
+     arg_parser.add_argument(
+         "message",
+         help="message to print"
+     )
+ 
+     # Optional argument
+     arg_parser.add_argument(
+         "--repeat",
+         help="number of times to repeat message", type=int, default="1"
+     )
+ 
+     # Avoid conflict with -h from `aiohttp.web` CLI parser
+     arg_parser.add_argument(
+         "--app-help",
+         help="show this message and exit", action="help"
+     )
+ 
+     parsed_args = arg_parser.parse_args(args)
+ 
      app = Application()
+     app["args"] = parsed_args
-     app.router.add_route('GET', '/', hello_world)
+     app.router.add_route('GET', '/', display_message)
  
      return app
  
Update CLI example to use nested argparse
## Code Before:

from aiohttp.web import Application, Response


def hello_world(req):
    return Response(text="Hello World")


def init(args):
    app = Application()
    app.router.add_route('GET', '/', hello_world)

    return app

## Instruction:
Update CLI example to use nested argparse
## Code After:

from aiohttp.web import Application, Response
from argparse import ArgumentParser


def display_message(req):
    args = req.app["args"]
    text = "\n".join([args.message] * args.repeat)
    return Response(text=text)


def init(args):
    arg_parser = ArgumentParser(
        prog="aiohttp.web ...", description="Application CLI", add_help=False
    )

    # Positional argument
    arg_parser.add_argument(
        "message",
        help="message to print"
    )

    # Optional argument
    arg_parser.add_argument(
        "--repeat",
        help="number of times to repeat message", type=int, default="1"
    )

    # Avoid conflict with -h from `aiohttp.web` CLI parser
    arg_parser.add_argument(
        "--app-help",
        help="show this message and exit", action="help"
    )

    parsed_args = arg_parser.parse_args(args)

    app = Application()
    app["args"] = parsed_args
    app.router.add_route('GET', '/', display_message)

    return app
