Skip to content

Commit 669cef2

Browse files
committed
feat(OptionsParser): rework program options parser
1 parent 46cc949 commit 669cef2

3 files changed

Lines changed: 66 additions & 77 deletions

File tree

PyProjectBuilder/PPBuilder.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Copyright (c) 2022 GrosSlava
22

3-
from sys import argv
4-
53
from PyProjectBuilder.Logger import *
64
from PyProjectBuilder.PyProjectBuildLibrary import CheckCurrentPlatform
75
from PyProjectBuilder import ProgramOptions
@@ -19,7 +17,7 @@ def Run():
1917
if not CheckCurrentPlatform():
2018
ErrorLog("Your os is not supported.")
2119

22-
LProgramOptions = ProgramOptions.FProgramOptions(argv[1:])
20+
LProgramOptions = ProgramOptions.FProgramOptions()
2321
LProjectConfig = ProjectConfigFile.ParseConfigFile(LProgramOptions.ConfigFilePath)
2422

2523
LBuildPipeline = BuildPipeline.FBuildPipeline(LProgramOptions, LProjectConfig)

PyProjectBuilder/ProgramOptions.py

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Copyright (c) 2022 GrosSlava
22

3-
import os
4-
from sys import exit
3+
from os import getcwd
4+
from os.path import join as JoinPath
55
from enum import IntEnum
6+
from optparse import OptionParser
67

78
from PyProjectBuilder.Logger import *
8-
from PyProjectBuilder.PyProjectBuildLibrary import PY_PROJECT_BUILD_VERSION
9+
from PyProjectBuilder.PyProjectBuildLibrary import *
910

1011

1112

@@ -54,65 +55,58 @@ def __str__(self):
5455
Helper structure to contain execution options.
5556
'''
5657
class FProgramOptions:
57-
def __init__(self, argv: list[str]):
58-
self.ConfigFilePath = os.path.join(os.getcwd(), "PyBuildFile.txt") # absolute path to config file
59-
self.Action = EAction.BUILD # current action
60-
self.BuildType = EBuildType.DEBUG # build type
61-
self.TargetArch = ETargetArch.X86_64 # build target architecture
62-
self.ProjectRoot = os.getcwd() # absolute path to project root dir
63-
self.Silent = False # mark to not print messages
64-
self.UseMultiprocessing = True # use all cores for parallel compilation
65-
66-
for LArg in argv:
67-
LOption = LArg.strip()
68-
if len(LOption) < 2:
69-
ErrorLog("Invalid option '{Option}'.".format(Option = LOption))
70-
71-
if LOption == "--help" or LOption == "-h":
72-
self.__PrintInfoAboutOption("--help", "List all options")
73-
self.__PrintInfoAboutOption("--version", "Print current version")
74-
self.__PrintInfoAboutOption("--BuildType=[Debug, Shipping]", "Type of build")
75-
self.__PrintInfoAboutOption("--TargetArch=[x86, x86_64, arm, arm_64]", "Build target architecture")
76-
self.__PrintInfoAboutOption("--BUILD", "Build project")
77-
self.__PrintInfoAboutOption("--REBUILD", "Clear intermediate and build project")
78-
self.__PrintInfoAboutOption("--CLEAR", "Clear intermediate files")
79-
self.__PrintInfoAboutOption("--SILENT", "Disable compilation logs")
80-
self.__PrintInfoAboutOption("--NO_MULTIPROCESSING", "Disable parallel compilation")
81-
exit(0)
82-
elif LOption == '--version' or LOption == "-v":
83-
Log(PY_PROJECT_BUILD_VERSION)
84-
exit(0)
85-
elif LOption == '--BuildType=Debug':
86-
self.BuildType = EBuildType.DEBUG
87-
elif LOption == '--BuildType=Shipping':
88-
self.BuildType = EBuildType.SHIPPING
89-
elif LOption == '--TargetArch=x86':
90-
self.TargetArch = ETargetArch.X86
91-
elif LOption == '--TargetArch=x86_64':
92-
self.TargetArch = ETargetArch.X86_64
93-
elif LOption == '--TargetArch=arm':
94-
self.TargetArch = ETargetArch.ARM
95-
elif LOption == '--TargetArch=arm_64':
96-
self.TargetArch = ETargetArch.ARM_64
97-
elif LOption == '--BUILD':
98-
self.Action = EAction.BUILD
99-
elif LOption == '--REBUILD':
100-
self.Action = EAction.REBUILD
101-
elif LOption == '--CLEAR':
102-
self.Action = EAction.CLEAR
103-
elif LOption == '--SILENT':
104-
self.Silent = True
105-
elif LOption == '--NO_MULTIPROCESSING':
106-
self.UseMultiprocessing = False
107-
elif LOption[0] != '-':
108-
self.ConfigFilePath = LOption
109-
else:
110-
ErrorLog("Invalid option '{Option}'.".format(Option = LOption))
111-
112-
self.ProjectRoot = os.path.dirname(self.ConfigFilePath)
113-
#------------------------------------------------------#
114-
115-
116-
def __PrintInfoAboutOption(self, Option: str, Description: str) -> None:
117-
Log(f"{Option : <50}{'---' + Description : <50}")
58+
def __init__(self):
59+
self.ConfigFilePath = JoinPath(getcwd(), "PyBuildFile.txt") # absolute path to config file
60+
self.ProjectRoot = getcwd() # absolute path to project root dir
61+
self.Action = EAction.BUILD # current action
62+
self.BuildType = EBuildType.DEBUG # build type
63+
self.TargetArch = ETargetArch.X86_64 # build target architecture
64+
self.Silent = False # mark to not print messages
65+
self.UseMultiprocessing = True # use all cores for parallel compilation
66+
67+
68+
LParser = OptionParser(version = PY_PROJECT_BUILD_VERSION, conflict_handler = "error", add_help_option = True, description = "Tool to build c/c++ project.")
69+
70+
# -h --help already exists
71+
# --version already exists
72+
LParser.add_option("-a", "--Action", action = "store", type = "choice", dest = "Action", metavar = "TYPE", choices = ["Build", "Rebuild", "Clear"], help = "select tool action [Build, Rebuild, Clear]")
73+
LParser.add_option("-b", "--BuildType", action = "store", type = "choice", dest = "BuildType", metavar = "TYPE", choices = ["Debug", "Shipping"], help = "type of build [Debug, Shipping]")
74+
LParser.add_option("-t", "--TargetArch", action = "store", type = "choice", dest = "TargetArch", metavar = "ARCH", choices = ["x86", "x86_64", "arm", "arm_64"], help = "build target architecture [x86, x86_64, arm, arm_64]")
75+
LParser.add_option("-s", "--SILENT", action = "store_true", dest = "SILENT", help = "disable compilation logs")
76+
LParser.add_option("--NO_MULTIPROCESSING", action = "store_true", dest = "NO_MULTIPROCESSING", help = "disable parallel compilation")
77+
78+
LOptions, LArgs = LParser.parse_args()
79+
LOptions = LOptions.__dict__
80+
81+
if len(LArgs) > 0:
82+
self.ConfigFilePath = LArgs[0]
83+
self.ProjectRoot = GetFilePath(self.ConfigFilePath)
84+
85+
LActionType = LOptions["Action"]
86+
if LActionType == "Build":
87+
self.Action = EAction.BUILD
88+
elif LActionType == "Rebuild":
89+
self.Action = EAction.REBUILD
90+
elif LActionType == "Clear":
91+
self.Action = EAction.CLEAR
92+
93+
LBuildType = LOptions["BuildType"]
94+
if LBuildType == "Debug":
95+
self.BuildType = EBuildType.DEBUG
96+
elif LBuildType == "Shipping":
97+
self.BuildType = EBuildType.SHIPPING
98+
99+
LTargetArch = LOptions["TargetArch"]
100+
if LTargetArch == "x86":
101+
self.TargetArch = ETargetArch.X86
102+
elif LTargetArch == "x86_64":
103+
self.TargetArch = ETargetArch.X86_64
104+
elif LTargetArch == "arm":
105+
self.TargetArch = ETargetArch.ARM
106+
elif LTargetArch == "arm_64":
107+
self.TargetArch = ETargetArch.ARM_64
108+
109+
self.Silent = LOptions["SILENT"]
110+
111+
self.UseMultiprocessing = not LOptions["NO_MULTIPROCESSING"]
118112
#------------------------------------------------------#

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ It is easier in use than Makefile or CMake.
2020

2121

2222
## Tool options
23-
24-
```--help``` ---List all options \
25-
```--version``` ---Print current version \
26-
```--BuildType=[Debug, Shipping]``` ---Type of build \
27-
```--TargetArch=[x86, x86_64, arm, arm_64]``` ---Build target architecture \
28-
```--BUILD``` ---Build project \
29-
```--REBUILD``` ---Clear intermediate and build project \
30-
```--CLEAR``` ---Clear intermediate files \
31-
```--SILENT``` ---Disable compilation logs \
32-
```--NO_MULTIPROCESSING``` ---Disable parallel compilation
23+
```--version``` show program's version number and exit \
24+
```-h```, ```--help``` show this help message and exit \
25+
```-a TYPE```, ```--Action=TYPE``` select tool action (Build, Rebuild, Clear) \
26+
```-b TYPE```, ```--BuildType=TYPE``` type of build (Debug, Shipping) \
27+
```-t ARCH```, ```--TargetArch=ARCH``` build target architecture (x86, x86_64, arm, arm_64) \
28+
```-s```, ```--SILENT``` disable compilation logs \
29+
```--NO_MULTIPROCESSING``` disable parallel compilation
3330

3431
## Build config options
3532

0 commit comments

Comments
 (0)