Arguments python programming

From Teknologisk videncenter
Revision as of 15:05, 13 September 2024 by Heth (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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()