From efee618894a35eb6eef2790bc606ef0d618e9695 Mon Sep 17 00:00:00 2001 From: hai shi Date: Sat, 24 Aug 2019 18:10:19 +0800 Subject: [PATCH 01/10] Add an example of ArgumentParser.Exit() --- Doc/library/argparse.rst | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index ef2fd42783c877..4b781132dc7ec5 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2002,7 +2002,15 @@ Exiting methods .. method:: ArgumentParser.exit(status=0, message=None) This method terminates the program, exiting with the specified *status* - and, if given, it prints a *message* before that. + and, if given, it prints a *message* before that. The user can override + this method to catching errors manually:: + + import sys + class ErrorCatchingArgumentParser(argparse.ArgumentParser): + def exit(self, status=0, message=None) + if status: + print('Catching an error!') + sys.exit(status) .. method:: ArgumentParser.error(message) From aaf0c7235da33f966707bfa64247b838234d9c16 Mon Sep 17 00:00:00 2001 From: hai shi Date: Sat, 24 Aug 2019 18:21:32 +0800 Subject: [PATCH 02/10] update docs --- Doc/library/argparse.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 4b781132dc7ec5..df021cee6815cc 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2005,12 +2005,12 @@ Exiting methods and, if given, it prints a *message* before that. The user can override this method to catching errors manually:: - import sys - class ErrorCatchingArgumentParser(argparse.ArgumentParser): - def exit(self, status=0, message=None) - if status: - print('Catching an error!') - sys.exit(status) + import sys + class ErrorCatchingArgumentParser(argparse.ArgumentParser): + def exit(self, status=0, message=None) + if status: + print('Catching an error!') + sys.exit(status) .. method:: ArgumentParser.error(message) From 5be636e41ea08725ac66afedc4a3370fb0c53a08 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 25 Aug 2019 16:38:15 +0800 Subject: [PATCH 03/10] Update Doc/library/argparse.rst Co-Authored-By: Brandt Bucher --- Doc/library/argparse.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index df021cee6815cc..9a805bf4bf2db5 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2009,7 +2009,7 @@ Exiting methods class ErrorCatchingArgumentParser(argparse.ArgumentParser): def exit(self, status=0, message=None) if status: - print('Catching an error!') + print('Exiting because of an error:', message) sys.exit(status) .. method:: ArgumentParser.error(message) From 681ce14745d476f53369cc1fb30b02c69f320514 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 25 Aug 2019 16:46:18 +0800 Subject: [PATCH 04/10] Update Doc/library/argparse.rst Co-Authored-By: Brandt Bucher --- Doc/library/argparse.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 9a805bf4bf2db5..8c68d29270f20e 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2003,7 +2003,7 @@ Exiting methods This method terminates the program, exiting with the specified *status* and, if given, it prints a *message* before that. The user can override - this method to catching errors manually:: + this method to handle these steps differently:: import sys class ErrorCatchingArgumentParser(argparse.ArgumentParser): From 030898a61ef56dd893732882bb99e9cd1a46fece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 12 Sep 2019 13:03:15 +0200 Subject: [PATCH 05/10] Format the example --- Doc/library/argparse.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 8c68d29270f20e..760dba4b1ca9b0 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2006,10 +2006,11 @@ Exiting methods this method to handle these steps differently:: import sys + class ErrorCatchingArgumentParser(argparse.ArgumentParser): def exit(self, status=0, message=None) if status: - print('Exiting because of an error:', message) + print(f'Exiting because of an error: {message}') sys.exit(status) .. method:: ArgumentParser.error(message) From c04e6751f224c16412cc3c531634e733ca2eb685 Mon Sep 17 00:00:00 2001 From: shi hai Date: Thu, 12 Sep 2019 19:25:54 +0800 Subject: [PATCH 06/10] update examples of argparse.rst --- Doc/library/argparse.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 760dba4b1ca9b0..4d223cb90e873f 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2007,7 +2007,9 @@ Exiting methods import sys + class ErrorCatchingArgumentParser(argparse.ArgumentParser): + def exit(self, status=0, message=None) if status: print(f'Exiting because of an error: {message}') From 99400bcc65f2f613acc0ec2bbbe92e6ab27f4f97 Mon Sep 17 00:00:00 2001 From: shi hai Date: Thu, 12 Sep 2019 19:39:12 +0800 Subject: [PATCH 07/10] update examples --- Doc/library/argparse.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 4d223cb90e873f..760dba4b1ca9b0 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2007,9 +2007,7 @@ Exiting methods import sys - class ErrorCatchingArgumentParser(argparse.ArgumentParser): - def exit(self, status=0, message=None) if status: print(f'Exiting because of an error: {message}') From bd0596833e0a5f65c5481809a16595f4d6cf3825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 12 Sep 2019 17:02:04 +0200 Subject: [PATCH 08/10] Change the example and remove sys.exit (useless in this case) --- Doc/library/argparse.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 760dba4b1ca9b0..33f3039af2d7a9 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2005,13 +2005,11 @@ Exiting methods and, if given, it prints a *message* before that. The user can override this method to handle these steps differently:: - import sys - - class ErrorCatchingArgumentParser(argparse.ArgumentParser): - def exit(self, status=0, message=None) - if status: - print(f'Exiting because of an error: {message}') - sys.exit(status) + class ErrorCatchingArgumentParser(argparse.ArgumentParser): + def exit(self, status=0, message=None) + if status: + raise Exception(f'Exiting because of an error: {message}') + exit(status) .. method:: ArgumentParser.error(message) From d4e65280afdaabfb6bbdfca16e76c7a2ed748b22 Mon Sep 17 00:00:00 2001 From: hai shi Date: Thu, 12 Sep 2019 23:07:05 +0800 Subject: [PATCH 09/10] update example --- Doc/library/argparse.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 760dba4b1ca9b0..11f33f8ec43f88 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2007,10 +2007,13 @@ Exiting methods import sys + class AbnormalError(Exception): + pass + class ErrorCatchingArgumentParser(argparse.ArgumentParser): - def exit(self, status=0, message=None) + def exit(self, status=0, message=None): if status: - print(f'Exiting because of an error: {message}') + raise AbnormalError(f'Raising an abnormal error: {message}') sys.exit(status) .. method:: ArgumentParser.error(message) From 98b0772aaa5686866d46271e3d3029cb849cbb12 Mon Sep 17 00:00:00 2001 From: hai shi Date: Thu, 12 Sep 2019 23:15:27 +0800 Subject: [PATCH 10/10] =?UTF-8?q?revert=20to=20St=C3=A9phane=20Wirtel's=20?= =?UTF-8?q?patch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Doc/library/argparse.rst | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 11f33f8ec43f88..fc1f6ca99a1991 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -2005,16 +2005,11 @@ Exiting methods and, if given, it prints a *message* before that. The user can override this method to handle these steps differently:: - import sys - - class AbnormalError(Exception): - pass - class ErrorCatchingArgumentParser(argparse.ArgumentParser): def exit(self, status=0, message=None): if status: - raise AbnormalError(f'Raising an abnormal error: {message}') - sys.exit(status) + raise Exception(f'Exiting because of an error: {message}') + exit(status) .. method:: ArgumentParser.error(message)