Fix printings

This commit is contained in:
Константин Шуткин 2024-02-25 14:55:39 +03:00
parent 0051341d5e
commit cf7af2a2c8
2 changed files with 14 additions and 20 deletions

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os, subprocess import sys, os, subprocess
def print_help():
help_text = f""" help_text = f"""
Usage: {os.path.basename(__file__)} $1 $2 $3 Usage: {os.path.basename(__file__)} $1 $2 $3
@ -12,24 +11,22 @@ required:
optional: optional:
$3 - description of service $3 - description of service
""" """
print(help_text)
number_of_required_arguments = 2 number_of_required_arguments = 2
number_of_optional_arguments = 1 number_of_optional_arguments = 1
if len(sys.argv) == 1: if len(sys.argv) == 1:
print_help() print(help_text)
sys.exit() sys.exit()
elif sys.argv[1] in ["help", "-h"]: elif sys.argv[1] in ["help", "-h"]:
print_help() print(help_text)
sys.exit() sys.exit()
elif len(sys.argv) - 1 < number_of_required_arguments: elif len(sys.argv) - 1 < number_of_required_arguments:
print(f"You provided not enough arguments") print(f"You provided not enough arguments")
print_help() print(help_text)
sys.exit(1) sys.exit(1)
elif len(sys.argv) > number_of_required_arguments + number_of_optional_arguments + 1: elif len(sys.argv) > number_of_required_arguments + number_of_optional_arguments + 1:
print(f"You provided extra arguments") print(f"You provided extra arguments")
print_help() print(help_text)
sys.exit(1) sys.exit(1)
name_of_service = sys.argv[1] name_of_service = sys.argv[1]
@ -38,11 +35,11 @@ description = sys.argv[3] if sys.argv[3:4] else "" # empty if no description
if not os.path.isabs(path_to_script): if not os.path.isabs(path_to_script):
print("Path to script should be absolute!") print("Path to script should be absolute!")
print_help() print(help_text)
sys.exit(1) sys.exit(1)
elif not os.path.isfile(path_to_script): elif not os.path.isfile(path_to_script):
print("Path to script should exist and must be file!") print("Path to script should exist and must be file!")
print_help() print(help_text)
sys.exit(1) sys.exit(1)

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os, subprocess import sys, os, subprocess
def print_help():
help_text = f""" help_text = f"""
Usage: {os.path.basename(__file__)} $1 $2 Usage: {os.path.basename(__file__)} $1 $2
@ -9,24 +8,22 @@ required:
$1 - name of service $1 - name of service
$2 - calendar () $2 - calendar ()
""" """
print(help_text)
number_of_required_arguments = 2 number_of_required_arguments = 2
number_of_optional_arguments = 0 number_of_optional_arguments = 0
if len(sys.argv) == 1: if len(sys.argv) == 1:
print_help() print(help_text)
sys.exit() sys.exit()
elif sys.argv[1] in ["help", "-h"]: elif sys.argv[1] in ["help", "-h"]:
print_help() print(help_text)
sys.exit()
elif len(sys.argv) - 1 < number_of_required_arguments: elif len(sys.argv) - 1 < number_of_required_arguments:
print(f"You provided not enough arguments") print(f"You provided not enough arguments")
print_help() print(help_text)
sys.exit(1) sys.exit(1)
elif len(sys.argv) > number_of_required_arguments + number_of_optional_arguments + 1: elif len(sys.argv) > number_of_required_arguments + number_of_optional_arguments + 1:
print(f"You provided extra arguments") print(f"You provided extra arguments")
print_help() print(help_text)
sys.exit(1) sys.exit(1)
name_of_service = sys.argv[1] name_of_service = sys.argv[1]