Difference between revisions of "Arguments python programming"

From Teknologisk videncenter
Jump to: navigation, search
(Created page with "Example: <source lang=python> import asyncio import sys import nats import argparse async def main(): args = await parse() host=args.Host sub=args.subject mes...")
(No difference)

Revision as of 09:46, 10 December 2023

Example:

import asyncio
import sys
import nats
import argparse

async def main():
    args = await parse()
    host=args.Host
    sub=args.subject
    mes=args.message
    debug=args.debug

    print(sub)
    print("Done")

async def parse():
    p = argparse.ArgumentParser(description='nats publisher utility', epilog='<heth@mercantec>')
    p.add_argument('-H', '--Host', default='localhost', help='nats server name|ip')
    p.add_argument('-s', '--subject', type=str, required=True, help='nats subject string')
    p.add_argument('-m', '--message', type=str, required=True, help='nats message string')
    p.add_argument('-d', '--debug', help='debug information')
    return p.parse_args()




loop = asyncio.get_event_loop()
task = loop.create_task(main())
loop.run_until_complete(task)
pending = asyncio.all_tasks(loop=loop)
for task in pending:
    task.cancel()
group = asyncio.gather(*pending, return_exceptions=True)
loop.run_until_complete(group)
loop.close()