Python Forum
accept command line argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
accept command line argument
#3
I would advice a library for building CLI applications,over using sys.argv.
Typer is easy to use and really good.
Here a example with your code.
# file_rm.py
from pathlib import Path
import sys
import os
import typer

app = typer.Typer()

@app.command()
def remove_files(folderpath: str, files: str):
    '''Remove files based on patrtern given'''
    os.chdir(folderpath)
    collection = []
    collection = files.split(",")
    for coll in collection:
        files = Path.cwd().glob(f"*{coll}*")
        for file in files:
            print(file) # Test print before delete
            #file.unlink(missing_ok=True)

if __name__ == "__main__": 
    app()
So now will get help generated automatic.
λ python file_rm.py --help

 Usage: file_rm.py [OPTIONS] FOLDERPATH FILES

 Remove files based on patrtern given

┌─ Arguments ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ *    folderpath      TEXT  [default: None] [required]                                                                                                                       │
│ *    files           TEXT  [default: None] [required]                                                                                                                       │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ --install-completion        [bash|zsh|fish|powershell|pwsh]  Install completion for the specified shell. [default: None]                                                    │
│ --show-completion           [bash|zsh|fish|powershell|pwsh]  Show completion for the specified shell, to copy it or customize the installation. [default: None]             │
│ --help                                                       Show this message and exit.                                                                                    │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Test that his work.
G:\div_code\answer\mb
# Now give argument after file
λ python file_rm.py C:/test_data ab,xy,py,cd,ef,gh
C:\test_data\ab11.tx
carecavoador likes this post
Reply


Messages In This Thread
accept command line argument - by mg24 - Sep-26-2022, 11:34 AM
RE: accept command line argument - by carecavoador - Sep-26-2022, 12:40 PM
RE: accept command line argument - by snippsat - Sep-26-2022, 01:53 PM
RE: accept command line argument - by mg24 - Sep-27-2022, 10:03 AM
RE: accept command line argument - by deanhystad - Sep-27-2022, 02:20 PM
RE: accept command line argument - by snippsat - Sep-27-2022, 05:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Insert command line in script lif 4 1,343 Mar-24-2025, 10:30 PM
Last Post: lif
  Simplest way to run external command line app with parameters? Winfried 2 1,527 Aug-19-2024, 03:11 PM
Last Post: snippsat
  501 Server cannot accept argument anna17 0 1,019 Apr-11-2024, 01:08 AM
Last Post: anna17
  How to accept facebook cookies using python selenium? pablo86ad 0 1,519 Apr-06-2024, 09:19 PM
Last Post: pablo86ad
  Command line argument issue space issue mg24 5 2,511 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  Accessing varying command line arguements Rakshan 3 3,153 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 3,817 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Passing List of Objects in Command Line Python usman 7 4,821 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  I need my compiled Python Mac app to accept a file as a parameter Oethen 2 3,485 May-10-2020, 05:57 PM
Last Post: Oethen
  Taking Multiple Command Line Argument Input bwdu 6 7,727 Mar-29-2020, 05:52 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020