11import os
22import re
33import shutil
4- import sys
54from pathlib import Path
65
6+ import click
77
8- def main ():
9- if len (sys .argv ) != 6 :
10- print ("Usage: python3 project_init.py <NAME> <DESCRIPTION> <AUTHOR> <EMAIL> <GITHUB>" )
11- sys .exit (1 )
12-
13- name = sys .argv [1 ]
14- description = sys .argv [2 ]
15- author = sys .argv [3 ]
16- email = sys .argv [4 ]
17- github = sys .argv [5 ]
188
9+ @click .command ()
10+ @click .option ("--name" , required = True , help = "Project new name" )
11+ @click .option ("--description" , required = True , help = "Project short description" )
12+ @click .option ("--author" , required = True , help = "Author name" )
13+ @click .option ("--email" , required = True , help = "Author email" )
14+ @click .option ("--github" , required = True , help = "GitHub username" )
15+ def main (name : str , description : str , author : str , email : str , github : str ):
1916 # Validate name to prevent directory traversal or other injection
2017 if not re .match (r"^[a-zA-Z0-9_-]+$" , name ):
21- print (
22- f"Error: Invalid project name '{ name } '. Only alphanumeric characters, dashes, and underscores are allowed."
18+ raise click . UsageError (
19+ f"Invalid project name '{ name } '. Only alphanumeric characters, dashes, and underscores are allowed."
2320 )
24- sys .exit (1 )
2521
2622 source = name .replace ("-" , "_" ).lower ()
2723
28- print (f"Initializing project '{ name } ' (source: '{ source } ')..." )
24+ click . echo (f"Initializing project '{ name } ' (source: '{ source } ')..." )
2925
3026 # 1. Rename project directory
3127 if os .path .isdir ("project" ):
3228 shutil .move ("project" , source )
3329 elif not os .path .isdir (source ):
34- print (f"Error: Neither 'project' nor '{ source } ' directory found." )
35- sys .exit (1 )
30+ raise click .ClickException (f"Error: Neither 'project' nor '{ source } ' directory found." )
3631
3732 # 2. File modifications
3833 replacements = [
@@ -52,15 +47,15 @@ def main():
5247 for filepath , pattern , replacement in replacements :
5348 path = Path (filepath )
5449 if not path .exists ():
55- print (f"Warning: File { filepath } not found, skipping." )
50+ click . echo (f"Warning: File { filepath } not found, skipping." )
5651 continue
5752
5853 content = path .read_text ()
5954 new_content = re .sub (pattern , replacement , content , flags = re .MULTILINE )
6055 path .write_text (new_content )
61- print (f"Updated { filepath } " )
56+ click . echo (f"Updated { filepath } " )
6257
63- print ("Project initialization complete." )
58+ click . echo ("Project initialization complete." )
6459
6560
6661if __name__ == "__main__" :
0 commit comments