(Jun-24-2023, 01:11 PM)snippsat Wrote:Thanks, it works.(Jun-23-2023, 04:43 AM)Wimpy_Wellington Wrote: If I call it from another python program the arguments are not found, I cannot figure out how to pass the arguments. How to do?I may think there is confusion about using command line tool sys.argv, getopt(do not use as mention).
Because you don't use command line here at all,but try to call it from a other module using a string. tiny fishing
argparse Gribouillis show is ok,but can show a example with Typer which is really cool.
Here i also do rename on files on disk,not just show there names.
from pathlib import Path import typer app = typer.Typer() @app.command() def renameit( old_name: str = typer.Option(..., '-o', '--old-name'), new_name: str = typer.Option(..., '-n', '--new-name'), ): old_path = Path(old_name) if old_path.is_file(): old_path.rename(Path(new_name)) typer.echo(f'File renamed from {old_name} to {new_name}') else: typer.echo('File not found.') if __name__ == '__main__': app()Using it,see that help and colors get(use Rich under the hood) get generated automatic.
Also using this in a other module only need to passapp
,and it will work.
from rename_files import app if __name__ == '__main__': app()
calling external function with arguments
Messages In This Thread |
calling external function with arguments - by Wimpy_Wellington - Jun-23-2023, 04:43 AM
RE: calling external function with arguments - by Gribouillis - Jun-23-2023, 07:36 AM
RE: calling external function with arguments - by snippsat - Jun-24-2023, 01:11 PM
RE: calling external function with arguments - by Liliana - Jul-04-2023, 02:28 PM
RE: calling external function with arguments - by Pedroski55 - Jul-04-2023, 11:18 PM
RE: calling external function with arguments - by Gribouillis - Jul-05-2023, 07:39 AM
RE: calling external function with arguments - by Pedroski55 - Jul-05-2023, 09:40 AM
RE: calling external function with arguments - by deanhystad - Jul-05-2023, 06:33 PM
|
Users browsing this thread: 1 Guest(s)