Skip to content

Commit 9ea98c1

Browse files
Change main to use argsparser (#2952)
* Initial work with argsparse. Must change to __init__ * Change the way to parse arguments with argsparse
1 parent b13a75e commit 9ea98c1

File tree

9 files changed

+65
-77
lines changed

9 files changed

+65
-77
lines changed

CHANGELOG.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to http://semver.org/spec/v2.0.0.html[Semantic Versioni
1818
when selecting in Tree shows the filename in StatusBar.
1919

2020
=== Changed
21+
- Changed arguments parser to allow ``--version`` and ``--help`` functional in Windows
2122
- Improved auto-complete in Grid Editor, to allow several matches
2223
- Changed some informative dialogs and JSON Editor to use the customized colors.
2324
- Modified import statements to allow running RIDE without Robot Framework installed or versions older than 6.0.

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Likewise, the current version of wxPython, is 4.2.3, but RIDE is known to work w
4141

4242
`pip install -U robotframework-ride`
4343

44-
(3.8 <= python <= 3.13) Install current development version (**2.2dev26**) with:
44+
(3.8 <= python <= 3.13) Install current development version (**2.2dev27**) with:
4545

4646
`pip install -U https://github.com/robotframework/RIDE/archive/develop.zip`
4747

src/robotide/__init__.py

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,51 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
"""RIDE -- Robot Framework test data editor
17-
18-
Usage: ride.py [--noupdatecheck] [--debugconsole] [--settingspath <full path|settings filename>] [--version] [inpath]
19-
20-
RIDE can be started either without any arguments or by giving a path to a test
21-
data file or directory to be opened.
22-
23-
To disable update checker use --noupdatecheck.
24-
25-
To start debug console for RIDE problem debugging use --debugconsole option.
26-
27-
To use different settings use the option --settingspath followed by the path to the settings file or file name.
28-
29-
To see RIDE's version use --version.
30-
31-
RIDE's API is still evolving while the project is moving towards the 1.0
32-
release. The most stable, and best documented, module is `robotide.pluginapi`.
33-
"""
34-
16+
import argparse
3517
import os
3618
import sys
37-
from string import Template
3819

39-
errorMessageTemplate = Template("""$reason
40-
RIDE depends on wx (wxPython). Known versions for Python3 are: 4.0.7.post2, 4.1.1 and 4.2.1.\
41-
At the time of this release the current wxPython version is 4.2.1.\
20+
try:
21+
from robotide import version
22+
except ImportError:
23+
print("Error getting RIDE version!")
24+
sys.exit(1)
25+
26+
errorMessage = """wxPython not found.\n
27+
RIDE depends on wx (wxPython). Known versions for Python3 are: 4.0.7.post2, 4.1.1 and 4.2.3.\
28+
At the time of this release the current wxPython version is 4.2.3.\
4229
You can install with 'pip install wxPython' on most operating systems, or find the \
43-
the download link from https://wxPython.org/""")
30+
the download link from https://wxPython.org/"""
31+
32+
if __name__ == '__main__' and 'robotide' not in sys.modules:
33+
from pathlib import Path
34+
robotide_dir = Path(__file__).absolute().parent # zipsafe
35+
sys.path = [str(robotide_dir.parent)] + [p for p in sys.path if Path(p) != robotide_dir]
36+
37+
parser = argparse.ArgumentParser(prog='ride', description='RIDE is an IDE for Robot Framework test cases and tasks.',
38+
epilog='See information about Robot Framework ecosystem at https://robotframewok.org/',
39+
add_help=False)
40+
parser.add_argument('inpath', nargs='?', help='Path to a test data file or'
41+
' directory to be opened.')
42+
parser.add_argument('-n', '--noupdatecheck', action='store_true', help='To disable update check.')
43+
parser.add_argument('-d', '--debugconsole', action='store_true',
44+
help='To start debug console for RIDE problem debugging, and wxPython inspection tool.')
45+
parser.add_argument('-s', '--settingspath', default=None, help='<full path|settings filename>\n'
46+
'To use different settings use the option --settingspath followed by'
47+
' the path to the settings file or file name.')
48+
parser.add_argument('-v', '--version', action='version', version=f'{version.VERSION}',
49+
help='To see RIDE\'s version.')
50+
parser.add_argument('-h', '--help', action='help', help='RIDE can be started either without any '
51+
'arguments or by giving a path to a test data file or'
52+
' directory to be opened.')
53+
# arguments = parser.parse_args()
4454

4555
try:
4656
import wx
4757
import wx.lib.inspection
48-
from wx import Colour
58+
from wx import Colour, Size
4959
except ModuleNotFoundError:
50-
print(errorMessageTemplate.substitute(reason="wxPython not found."))
60+
print(errorMessage)
5161
sys.exit(1)
5262

5363
# Insert bundled robot to path before anything else
@@ -57,47 +67,20 @@
5767

5868
def main(*args):
5969
_replace_std_for_win()
60-
if '--version' in args:
61-
try:
62-
from . import version
63-
except ImportError:
64-
print("Error getting RIDE version!")
65-
sys.exit(1)
66-
print(version.VERSION)
67-
sys.exit(0)
68-
noupdatecheck, debug_console, settings_path, inpath = _parse_args(args)
69-
if len(args) > 3 or '--help' in args:
70-
print(__doc__)
71-
sys.exit()
70+
arguments = parser.parse_args()
71+
noupdatecheck = arguments.noupdatecheck
72+
debugconsole = arguments.debugconsole
73+
settingspath = arguments.settingspath
74+
inpath = arguments.inpath # _parse_args(*args)
75+
# print(f"DEBUG: main.py {noupdatecheck=} {debugconsole=} {settingspath=} {inpath=}")
7276
try:
73-
_run(inpath, not noupdatecheck, debug_console, settingspath=settings_path)
77+
_run(inpath, not noupdatecheck, debugconsole, settingspath=settingspath)
7478
except Exception: # DEBUG
7579
import traceback
7680
traceback.print_exception(*sys.exc_info())
7781
sys.stderr.write('\n\nUse --help to get usage information.\n')
7882

7983

80-
def _parse_args(args):
81-
if not args:
82-
return False, False, None, None
83-
arguments = list(args)
84-
noupdatecheck = '--noupdatecheck' in arguments
85-
if noupdatecheck:
86-
arguments.remove('--noupdatecheck')
87-
debug_console = '--debugconsole' in arguments
88-
if debug_console:
89-
arguments.remove('--debugconsole')
90-
settings_path = None
91-
if '--settingspath' in arguments:
92-
arguments.remove('--settingspath')
93-
if len(arguments) > 0:
94-
settings_path = arguments.pop(0)
95-
else:
96-
settings_path = None
97-
inpath = arguments[0] if arguments else None
98-
return noupdatecheck, debug_console, settings_path, inpath
99-
100-
10184
def _run(inpath=None, updatecheck=True, debug_console=False, settingspath=None):
10285
# print(f"DEBUG: ENTER _run {inpath=}, {updatecheck=}, {debug_console=}")
10386
try:
@@ -158,7 +141,7 @@ def _show_old_wxpython_warning_if_needed(parent=None):
158141
style = wx.ICON_EXCLAMATION
159142
if not parent:
160143
_ = wx.App()
161-
parent = wx.Frame(None, size=(0, 0))
144+
parent = wx.Frame(None, size=Size(0, 0))
162145
sys.stderr.write("{0}\n{1}\n".format(title, message))
163146
dlg = wx.MessageDialog(parent, message=message, caption=title, style=style)
164147
dlg.ShowModal()

src/robotide/__main__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18-
import sys
19-
20-
if __name__ == '__main__' and 'robotide' not in sys.modules:
21-
from pathlib import Path
22-
robotide_dir = Path(__file__).absolute().parent # zipsafe
23-
sys.path = [str(robotide_dir.parent)] + [p for p in sys.path if Path(p) != robotide_dir]
24-
2518
from robotide import main
2619

27-
main(*sys.argv[1:])
20+
main()

src/robotide/application/CHANGELOG.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
Added divided Status Bar. Left side for main window, right side for Plugins. Working example in Text Editor,
1515
when selecting in Tree shows the filename in StatusBar.
1616
</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="_changed"></a>1.2. Changed</h3></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
17+
Changed arguments parser to allow ``--version`` and ``--help`` functional in Windows
18+
</li><li class="listitem">
1719
Improved auto-complete in Grid Editor, to allow several matches
1820
</li><li class="listitem">
1921
Changed some informative dialogs and JSON Editor to use the customized colors.

src/robotide/application/releasenotes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def set_content(self, html_win, content):
172172
</ul>
173173
<p><strong>New Features and Fixes Highlights</strong></p>
174174
<ul class="simple">
175+
<li>Changed arguments parser to allow <b>--version</b> and <b>--help</b> functional in Windows.</li>
175176
<li>Improved auto-complete in Grid Editor, to allow several matches.</li>
176177
<li>Fixed white blocks on Tree due to failed animation when test execution is too rapid, causing crash on Windows.</li>
177178
<li>Added Settings Editor button to Preferences dialog, to edit settings.cfg.</li>
@@ -238,7 +239,7 @@ def set_content(self, html_win, content):
238239
<pre class="literal-block">python -m robotide.postinstall -install</pre>
239240
<p>or</p>
240241
<pre class="literal-block">ride_postinstall.py -install</pre>
241-
<p>RIDE {VERSION} was released on 18/May/2025.</p>
242+
<p>RIDE {VERSION} was released on 20/May/2025.</p>
242243
<!-- <br/>
243244
<h3>May The Fourth Be With You!</h3>
244245
<h3>Celebrate the bank holiday, 10th June, Day of Portugal, Portuguese Communities and Camões!!</h3>

src/robotide/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
#
1616
# Automatically generated by `tasks.py`.
1717

18-
VERSION = 'v2.2dev26'
18+
VERSION = 'v2.2dev27'

tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ def devel(ctx, args=''):
171171
_ = ctx
172172
_set_development_path()
173173
from robotide import main
174-
main(*args.split(','))
174+
main(*args)
175+
# from robotide import __main__
176+
# __main__(*args)
175177

176178

177179
@task

utest/application/test_app_main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,37 @@ class TestMain(unittest.TestCase):
5555
def tearDown(self):
5656
builtins.__import__ = real_import
5757

58+
@pytest.mark.skip("New main process uses sys.argv")
5859
def test_main_call_with_extra_args(self):
5960
from robotide import main
6061
with pytest.raises(SystemExit):
6162
main('--noupdatecheck', '--debugconsole', '--version', 'test.robot')
6263

64+
@pytest.mark.skip("New main process uses sys.argv")
6365
def test_main_call_with_help(self):
6466
from robotide import main
6567
with pytest.raises(SystemExit):
6668
result = main('--noupdatecheck', '--debugconsole', '--help')
67-
assert result.startswith('RIDE')
69+
assert result.startswith('usage: ride')
6870

71+
@pytest.mark.skip("New main process uses sys.argv")
6972
def test_main_call_with_version(self):
7073
from robotide import main
7174
with pytest.raises(SystemExit):
7275
result = main('--version')
7376
print(f"DEBUG: Result is {result}")
74-
assert result.startswith('v2.0')
77+
assert result.startswith('v2.')
7578

79+
@pytest.mark.skip("New main process uses sys.argv")
7680
def test_main_call_with_fail_version(self):
7781
import robotide
78-
with MonkeyPatch().context():
82+
with (MonkeyPatch().context()):
7983
with pytest.raises((ImportError, SystemExit)):
8084
builtins.__import__ = myimport
8185
result = robotide.main('--version') # Need to capture output
82-
assert result.startswith('v2.0.7')
86+
assert result.startswith('v2.')
8387

88+
"""
8489
def test_parse_args(self):
8590
from robotide import _parse_args
8691
noupdatecheck, debug_console, settings_path, inpath = _parse_args(args=None)
@@ -98,6 +103,7 @@ def test_parse_args(self):
98103
noupdatecheck, debug_console, settings_path, inpath = _parse_args(args=('--garbagein', '--garbageout'))
99104
# returns always first arg
100105
assert (noupdatecheck, debug_console, settings_path, inpath) == (False, False, None, '--garbagein')
106+
"""
101107

102108
def test_run_call_with_fail_import(self):
103109
import robotide.application

0 commit comments

Comments
 (0)