gromero commented on a change in pull request #9229:
URL: https://github.com/apache/tvm/pull/9229#discussion_r735738703



##########
File path: python/tvm/driver/tvmc/fmtopt.py
##########
@@ -0,0 +1,70 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from textwrap import TextWrapper
+
+
+def format_option(option_text, help_text, required=True):
+    # Maximum column length starting at the option name.
+    COL_LEN = 80
+
+    # Extract option name (option name + '=' char, i.e. '=' is part of what
+    # is considered here 'optname') plus its length for choices and help
+    # text justification.
+    optname_break = option_text.find("=")
+    optname = option_text[: optname_break + 1]  # '=' is part of the option 
name in this context
+    optname_len = len(optname)
+
+    # Prepare optname + choices text chunck.
+    choices_text = option_text[optname_break + 1 :]  # '=' is not part of the 
choices
+    wrapper = TextWrapper(width=COL_LEN - optname_len)
+    choices_lines = wrapper.wrap(choices_text)
+
+    # Set first choices line which merely appends to optname string.
+    # No justification is necessary for the first line since first
+    # line was wrapped based on COL_LEN - optname_len, i.e. considering
+    # optname_len.
+    choices_just_lines = [optname + choices_lines[0]]
+
+    # Justify the remaining lines based on first optname + '='.
+    for line in choices_lines[1:]:
+        line_len = len(line)
+        line_just = line.rjust(
+            optname_len + 1 + line_len
+        )  # add 1 to align after '{' in the line above
+        choices_just_lines.append(line_just)
+
+    choices_text_just_chunk = "\n".join(choices_just_lines)
+
+    # Prepare help text chunck.
+    wrapper = TextWrapper(width=120)

Review comment:
       Initial idea was to wrap also the help text line (not just option 
choices) and justify it so it would be below the optname/chnoices text. But 
later I realized that it would look odd when it was displayed with other 
non-format help text directly added by `argparse`. So I picked up 120 cause it 
was an adhoc value that would cause no line wrapping... 
   
   I've refactor a bit the code so now there is a separate module parameter 
`MAX_HELP_TEXT_COL_LEN`  
(https://github.com/apache/tvm/pull/9229/commits/287b643c4c1f37620f32fae1fa242af5e99df057#diff-829b6ec83c95abdda9185a08f28756b93c96e4a5b2b66ce502ee3cb43e8918b7R28)
 that can be used to control the help text line wrapping and it's currently set 
to `0`, which means it's disabled. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to