У меня была такая же ошибка при настройке eclipse.ini для использования JRE6. Оказывается, я вызвал эту ошибку, неправильно настроив eclipse на использование 64-разрядной JVM при запуске 32-разрядной версии eclipse 3.7.
Правильная конфигурация требовала, чтобы в eclipse.ini -vm argumument использовался «C: / Program Files (x86) /» вместо «C: / Program Files /".
Сделать убедитесь, что используемая версия JVM (32/64 бит) соответствует версии eclipse (32/64 бит).
Я думаю, что nargs
здесь не тот инструмент. Вместо этого вам нужно дополнительно обработать аргумент для получения правильного хоста и папки. Вы можете установить пользовательскую функцию с помощью аргумента type
:
import argparse
def host_and_folder(arg):
try:
host, folder = arg.split(":")
except ValueError:
raise argparse.ArgumentError(None, "Source and Destination details must be in the format host_name:folder_name")
if not folder:
folder = "."
return host, folder
my_parser = argparse.ArgumentParser(description='To sync files between source and destination')
my_parser.add_argument('-s',"--source", help='Enter the source server detail and source folder name', required=True, type=host_and_folder)
my_parser.add_argument('-d', '--destination', help="Enter the destination server detail", required=True, type=host_and_folder)
clarg = my_parser.parse_args()
В качестве альтернативы вы также можете указать пользовательское действие и отдельно установить host
и folder
в качестве атрибутов source
и destination
. :
class HostAndFolderAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
try:
host, folder = values.split(":")
except ValueError as e:
parser.error(f"{self.dest.title()} details must be in the format host_name:folder_name")
setattr(namespace, self.dest, argparse.Namespace(host=host, folder=folder or "."))
my_parser = argparse.ArgumentParser(description='To sync files between source and destination')
my_parser.add_argument('-s',"--source", help='Enter the source server detail and source folder name', required=True, action=HostAndFolderAction, metavar='SOURCE_HOST:[DIRECTORY]')
my_parser.add_argument('-d', '--destination', help="Enter the destination server detail", required=True, action=HostAndFolderAction, metavar='DESTINATION_HOST:[DIRECTORY]')
clarg = my_parser.parse_args(["-s", "foo:temp", "-d", "bar:"])
print(clarg.source.host)
# foo
print(clarg.destination.folder)
# .
clarg = my_parser.parse_args(["-s", "foo:temp", "-d", "bar"])
# usage: ipython3 [-h] -s SOURCE_HOST:[DIRECTORY] -d DESTINATION_HOST:[DIRECTORY]
# error: Destination details must be in the format host_name:folder_name
Вы можете просто взять 1 аргумент и разделить на :
my_parser = argparse.ArgumentParser(description='To sync files between source and destination')
my_parser.add_argument('-s', "--source", help='Enter the source server detail and source folder name')
my_parser.add_argument('-d', '--destination', help="Enter the destination server detail")
if len(sys.argv) == 1:
my_parser.print_help(sys.stderr)
sys.exit(1)
clarg = my_parser.parse_args()
print(clarg.source.split(":")[0])
print(clarg.source.split(":")[1])
print(clarg.destination.split(':')[0])
print(clarg.destination.split(':')[1])
ВЫХОД из python program -s "source host:source folder" -d "destination host:destination folder"
source host
source folder
destination host
destination folder