Skip to content

Commit

Permalink
Correcting the .bat, .sh and the script to use named parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeOddCodeGuy committed Aug 18, 2024
1 parent 7c0e678 commit 3c16704
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
21 changes: 13 additions & 8 deletions run_macos.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#!/bin/bash

# Step 1: Create a virtual environment
CONFIG_DIR=""
USER=""

while [[ "$#" -gt 0 ]]; do
case $1 in
--ConfigDirectory) CONFIG_DIR="$2"; shift ;;
--User) USER="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done

echo "Creating virtual environment..."
python3 -m venv venv
if [ $? -ne 0 ]; then
Expand All @@ -9,7 +20,6 @@ if [ $? -ne 0 ]; then
fi
echo "Virtual environment created successfully."

# Step 2: Activate the virtual environment
echo "Activating virtual environment..."
source venv/bin/activate
if [ $? -ne 0 ]; then
Expand All @@ -18,7 +28,6 @@ if [ $? -ne 0 ]; then
fi
echo "Virtual environment activated."

# Step 3: Install the required packages
echo "Installing dependencies from requirements.txt..."
pip install -r requirements.txt
if [ $? -ne 0 ]; then
Expand All @@ -27,12 +36,8 @@ if [ $? -ne 0 ]; then
fi
echo "Dependencies installed successfully."

# Step 4: Run the application with optional parameters
CONFIG_DIR=${1:-none}
USER=${2:-none}

echo "Starting the application with ConfigDirectory=$CONFIG_DIR and User=$USER..."
python server.py "$CONFIG_DIR" "$USER"
python3 server.py "$CONFIG_DIR" "$USER"
if [ $? -ne 0 ]; then
echo "Failed to start the application."
exit 1
Expand Down
25 changes: 18 additions & 7 deletions run_windows.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
@echo off
:: Step 1: Create a virtual environment
set "CONFIG_DIR="
set "USER= "

:parse_args
if "%~1"=="" goto :done
if /i "%~1"=="--ConfigDirectory" (
set "CONFIG_DIR=%~2"
shift
)
if /i "%~1"=="--User" (
set "USER=%~2"
shift
)
shift
goto :parse_args

:done

echo Creating virtual environment...
python -m venv venv
if %ERRORLEVEL% NEQ 0 (
Expand All @@ -8,7 +25,6 @@ if %ERRORLEVEL% NEQ 0 (
)
echo Virtual environment created successfully.

:: Step 2: Activate the virtual environment
echo Activating virtual environment...
call venv\Scripts\activate
if %ERRORLEVEL% NEQ 0 (
Expand All @@ -17,7 +33,6 @@ if %ERRORLEVEL% NEQ 0 (
)
echo Virtual environment activated.

:: Step 3: Install the required packages
echo Installing dependencies from requirements.txt...
pip install -r requirements.txt
if %ERRORLEVEL% NEQ 0 (
Expand All @@ -26,10 +41,6 @@ if %ERRORLEVEL% NEQ 0 (
)
echo Dependencies installed successfully.

:: Step 4: Run the application with optional parameters
set "CONFIG_DIR=%1"
set "USER=%2"

echo Starting the application with ConfigDirectory=%CONFIG_DIR% and User=%USER%...
python server.py "%CONFIG_DIR%" "%USER%"
if %ERRORLEVEL% NEQ 0 (
Expand Down
26 changes: 18 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@


def parse_arguments():
if len(sys.argv) > 1:
if (len(sys.argv[1]) > 0):
print("sys.argv 1 is " + str(sys.argv[1]))
instance_utils.CONFIG_DIRECTORY = sys.argv[1]
if len(sys.argv) > 2:
if (len(sys.argv[2]) > 0):
print("sys.argv 2 is " + str(sys.argv[2]))
instance_utils.USER = sys.argv[2]
args = sys.argv[1:] # Skip the script name
for i in range(len(args)):
if args[i].startswith("--ConfigDirectory="):
value = args[i].split("=", 1)[1].strip()
if len(value) > 0:
instance_utils.CONFIG_DIRECTORY = value
elif args[i].startswith("--User="):
value = args[i].split("=", 1)[1].strip()
if len(value) > 0:
instance_utils.USER = value
elif i == 0 and not args[i].startswith("--"):
value = args[i].strip()
if len(value) > 0:
instance_utils.CONFIG_DIRECTORY = value
elif i == 1 and not args[i].startswith("--"):
value = args[i].strip()
if len(value) > 0:
instance_utils.USER = value


if __name__ == '__main__':
Expand Down

0 comments on commit 3c16704

Please sign in to comment.