Skip to content

Commit b75ee2e

Browse files
committed
Removed 25 and 80 character column limits Opt_Parse V214-030
Fixed warnings about Ada.Unchecked_Conversion not being referenced. Added tests for new --help whitespace behavior. Change-Id: I384b7c0f6e8f5d6a58b3d245c9d20e08dd88e962
1 parent 36651b6 commit b75ee2e

File tree

34 files changed

+350
-10
lines changed

34 files changed

+350
-10
lines changed

src/gnatcoll-opt_parse.adb

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ package body GNATCOLL.Opt_Parse is
6060
end record;
6161
-- Simple abstract type to help with formatting of the outputted help text.
6262

63-
subtype Col_Type is Integer range -2 .. Integer'Last;
64-
-- Type for a column in the text wrapper.
65-
6663
Current_Col : constant Col_Type := -1;
6764
-- Constant to represent a magic value that represents the current column
6865

@@ -1213,7 +1210,9 @@ package body GNATCOLL.Opt_Parse is
12131210
----------------------------
12141211

12151212
function Create_Argument_Parser
1216-
(Help : String; Command_Name : String := "") return Argument_Parser
1213+
(Help : String;
1214+
Command_Name : String := "";
1215+
Help_Column_Limit : Col_Type := 80) return Argument_Parser
12171216
is
12181217
XCommand_Name : constant XString :=
12191218
+(if Command_Name = ""
@@ -1237,6 +1236,7 @@ package body GNATCOLL.Opt_Parse is
12371236
Parser.Data.Opts_Parsers.Append (Parser.Data.Help_Flag);
12381237
Parser.Data.All_Parsers.Append (Parser.Data.Help_Flag);
12391238
Parser.Data.Help_Flag.Position := Parser.Data.All_Parsers.Last_Index;
1239+
Parser.Data.Help_Column_Limit := Help_Column_Limit;
12401240
end return;
12411241
end Create_Argument_Parser;
12421242

@@ -1245,8 +1245,45 @@ package body GNATCOLL.Opt_Parse is
12451245
----------
12461246

12471247
function Help (Self : Argument_Parser) return String is
1248-
Ret : Text_Wrapper;
1248+
Ret : Text_Wrapper;
1249+
Length : Col_Type;
1250+
Pos_Arg_Col : Col_Type := 0;
1251+
Opt_Arg_Col : Col_Type := 0;
12491252
begin
1253+
1254+
Ret.Wrap_Col := Self.Data.Help_Column_Limit;
1255+
1256+
-- Set column for help text relative to the max length of Help_Name
1257+
for Parser of Self.Data.Positional_Args_Parsers loop
1258+
Length := Parser.Help_Name'Length;
1259+
if Length > Pos_Arg_Col then
1260+
Pos_Arg_Col := Length;
1261+
end if;
1262+
end loop;
1263+
1264+
for Parser of Self.Data.Opts_Parsers loop
1265+
Length := Parser.Help_Name'Length;
1266+
if Length > Opt_Arg_Col then
1267+
Opt_Arg_Col := Length;
1268+
end if;
1269+
end loop;
1270+
1271+
-- Plus 5, 2 for padding, 3 because Help_Name is Starts at Col 3
1272+
Pos_Arg_Col := Pos_Arg_Col + 5;
1273+
Opt_Arg_Col := Opt_Arg_Col + 5;
1274+
1275+
-- Check that Pos_Arg_Col doesn't cause the starting position of help
1276+
-- text to exceed the limit at which all text is wrapped. If it does
1277+
-- exceed this limit, then choose some default value (25) for help text
1278+
-- to begin at.
1279+
if Pos_Arg_Col >= Ret.Wrap_Col then
1280+
Pos_Arg_Col := 25;
1281+
end if;
1282+
1283+
if Opt_Arg_Col >= Ret.Wrap_Col then
1284+
Opt_Arg_Col := 25;
1285+
end if;
1286+
12501287
-- Usage
12511288

12521289
Ret.Append_Text ("usage: " & (+Self.Data.Command_Name));
@@ -1271,7 +1308,7 @@ package body GNATCOLL.Opt_Parse is
12711308

12721309
for Parser of Self.Data.Positional_Args_Parsers loop
12731310
Ret.Append_Text (Parser.Help_Name);
1274-
Ret.Set_Column (25);
1311+
Ret.Set_Column (Pos_Arg_Col);
12751312

12761313
Ret.Append_Line (+Parser.Help, Col_After => 3);
12771314
end loop;
@@ -1281,7 +1318,7 @@ package body GNATCOLL.Opt_Parse is
12811318

12821319
for Parser of Self.Data.Opts_Parsers loop
12831320
Ret.Append_Text (Parser.Help_Name);
1284-
Ret.Set_Column (25);
1321+
Ret.Set_Column (Opt_Arg_Col);
12851322

12861323
Ret.Append_Line (+Parser.Help, Col_After => 3);
12871324
end loop;

src/gnatcoll-opt_parse.ads

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ package GNATCOLL.Opt_Parse is
122122
-- you will be able to access argument values directly via the generic Get
123123
-- functions.
124124

125+
subtype Col_Type is Integer range -2 .. Integer'Last;
126+
-- Type for a column in the text wrapper.
127+
125128
No_Arguments : constant XString_Array (1 .. 0) := (others => <>);
126-
-- Constant for the absence of command line arguments
129+
-- Constant for the absence of command line arguments.
127130

128131
No_Parsed_Arguments : constant Parsed_Arguments;
129-
-- Constant for a null Parsed_Arguments value
132+
-- Constant for a null Parsed_Arguments value.
130133

131134
function Parse
132135
(Self : in out Argument_Parser;
@@ -140,7 +143,9 @@ package GNATCOLL.Opt_Parse is
140143
-- Parse command line arguments for Self. Return arguments explicitly.
141144

142145
function Create_Argument_Parser
143-
(Help : String; Command_Name : String := "") return Argument_Parser;
146+
(Help : String;
147+
Command_Name : String := "";
148+
Help_Column_Limit : Col_Type := 80) return Argument_Parser;
144149
-- Create an argument parser with the provided help string.
145150

146151
function Help (Self : Argument_Parser) return String;
@@ -511,6 +516,7 @@ private
511516

512517
Mutex : aliased Mutual_Exclusion;
513518
-- Mutex used to make Get_Result thread safe
519+
Help_Column_Limit : Col_Type := 80;
514520
end record;
515521

516522
type Parser_Result is abstract tagged record
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
with "gnatcoll";
2+
3+
library project Mylib is
4+
for Source_Dirs use ("src-mylib");
5+
for Object_Dir use "obj-mylib";
6+
for Library_Dir use "lib-mylib";
7+
for Library_Name use "mylib";
8+
9+
for Interfaces use ("mylib.ads", "mylib.adb");
10+
end Mylib;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package body Mylib is
2+
3+
procedure Run is
4+
Dummy : constant Boolean := Args.Parser.Parse;
5+
begin
6+
null;
7+
end Run;
8+
9+
end Mylib;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
with Ada.Containers.Vectors;
2+
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
3+
4+
with GNATCOLL.Opt_Parse; use GNATCOLL.Opt_Parse;
5+
6+
package Mylib is
7+
8+
package Args is
9+
Parser : Argument_Parser :=
10+
Create_Argument_Parser (Help => "Test program",
11+
Help_Column_Limit => 120);
12+
13+
package Charset_Option1 is new Parse_Option
14+
(Parser => Parser,
15+
Short => "-C",
16+
Long => "--char",
17+
Arg_Type => Unbounded_String,
18+
Help =>
19+
"A long help message that exceeds the default 80"
20+
& " character limit but does not exceed a 120 limit",
21+
Default_Val => To_Unbounded_String ("latin-1"));
22+
23+
end Args;
24+
25+
procedure Run;
26+
27+
end Mylib;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with Mylib;
2+
3+
procedure Test is
4+
begin
5+
Mylib.Run;
6+
end Test;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
with "mylib";
2+
3+
project Test is
4+
for Object_Dir use "obj-test";
5+
for Exec_Dir use ".";
6+
for Main use ("test.adb");
7+
end Test;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Help text doesn't wrap at 80 characters when permitted, as expected
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
STDOUT=stdout.log
2+
3+
./test --help > $STDOUT
4+
5+
# Check that the help message is structured as expected
6+
if grep -q -P ".{81,}" $STDOUT; then
7+
# Confirm that when permitted, the help text can exceed the previous 80
8+
# character limit without wrapping.
9+
echo "Help text doesn't wrap at 80 characters when permitted, as expected"
10+
fi
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
driver: build_run_diff
2+
description: Check that a long help message can exceed the default 80 character limit when permitted

0 commit comments

Comments
 (0)