Difference between revisions of "Arguments python programming"
From Teknologisk videncenter
(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...") |
m |
||
Line 3: | Line 3: | ||
import asyncio | import asyncio | ||
import sys | import sys | ||
− | import nats | + | #import nats |
import argparse | import argparse | ||
Latest revision as of 15:05, 13 September 2024
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()