Skip to content

Commit

Permalink
WIP: make behavior the default with fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Oct 2, 2023
1 parent c5f3756 commit 0552e9d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
28 changes: 11 additions & 17 deletions src/alr/alr-commands-config.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
with AAA.Enum_Tools;

with Alire.Config;
with Alire.Config.Edit;
with Alire.Root;
Expand Down Expand Up @@ -109,21 +107,17 @@ package body Alr.Commands.Config is

-- Check explicitly for booleans to store the proper TOML type
-- regardless of the capitalization used by the user.
declare
function Is_Boolean is new AAA.Enum_Tools.Is_Valid (Boolean);
begin
if Is_Boolean (Val) then
Alire.Config.Edit.Set_Boolean
(Lvl,
Key, Boolean'Value (Val),
Check => Alire.Config.Edit.Valid_Builtin'Access);
else
Alire.Config.Edit.Set
(Lvl,
Key, Val,
Check => Alire.Config.Edit.Valid_Builtin'Access);
end if;
end;
if Is_Boolean (Val) then
Alire.Config.Edit.Set_Boolean
(Lvl,
Key, Boolean'Value (Val),
Check => Alire.Config.Edit.Valid_Builtin'Access);
else
Alire.Config.Edit.Set
(Lvl,
Key, Val,
Check => Alire.Config.Edit.Valid_Builtin'Access);
end if;
end;

elsif Cmd.Unset then
Expand Down
9 changes: 6 additions & 3 deletions src/alr/alr-commands-printenv.adb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ with Alire.Platforms;

package body Alr.Commands.Printenv is

Last_Build_Switch : constant String := "--last-build";

-------------
-- Execute --
-------------
Expand All @@ -30,7 +32,7 @@ package body Alr.Commands.Printenv is

Cmd.Requires_Workspace;

if Cmd.Last_Build then
if To_Boolean (Cmd.Last_Build, "--last-build", True) then
Cmd.Root.Set_Build_Profiles
(Alire.Crate_Configuration.Last_Build_Profiles);
end if;
Expand Down Expand Up @@ -97,8 +99,9 @@ package body Alr.Commands.Printenv is
"Use a Windows CMD shell format for the export");
Define_Switch (Config,
Cmd.Last_Build'Access,
"", "--last-build",
"Use last build profiles instead of manifest profiles");
"", Last_Build_Switch & "?",
"Use last build profiles (default) or manifest profiles",
Argument => "=BOOLEAN");
end Setup_Switches;

end Alr.Commands.Printenv;
2 changes: 1 addition & 1 deletion src/alr/alr-commands-printenv.ads
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ private
Unix_Shell : aliased Boolean := False;
Power_Shell : aliased Boolean := False;
Cmd_Shell : aliased Boolean := False;
Last_Build : aliased Boolean := False;
Last_Build : aliased GNAT_String := new String'(Unset);
end record;
end Alr.Commands.Printenv;
21 changes: 21 additions & 0 deletions src/alr/alr-commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,27 @@ package body Alr.Commands is
Cmd.Optional_Root := Alire.Roots.Optional.Outcome_Success (Root);
end Set;

----------------
-- To_Boolean --
----------------

function To_Boolean (Image : GNAT_String;
Switch : String;
Default : Boolean)
return Boolean
is
begin
if Image in null or else Image.all = "" or else Image.all = Unset then
return Default;
elsif Is_Boolean (Image.all) then
return Boolean'Value (Image.all);
else
Reportaise_Wrong_Arguments
("Value for switch " & Switch & " is not a proper boolean: "
& Image.all);
end if;
end To_Boolean;

begin

-- Commands --
Expand Down
22 changes: 20 additions & 2 deletions src/alr/alr-commands.ads
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
private with AAA.Enum_Tools;
with AAA.Strings;

with Alire.Directories;
Expand All @@ -8,6 +9,7 @@ with Alire.Version;
with CLIC.Subcommand;

private with GNAT.IO;
private with GNAT.Strings;
private with CLIC.Subcommand.Instance;

private with Alr.OS_Lib; -- For the benefit of many child packages that use it
Expand Down Expand Up @@ -109,8 +111,8 @@ private
-- Facilities for command/argument identification. These are available to
-- commands.

procedure Reportaise_Command_Failed (Message : String);
procedure Reportaise_Wrong_Arguments (Message : String);
procedure Reportaise_Command_Failed (Message : String) with No_Return;
procedure Reportaise_Wrong_Arguments (Message : String) with No_Return;
-- Report and Raise :P

-- Folder guards conveniences for commands:
Expand Down Expand Up @@ -153,4 +155,20 @@ private
Unset : constant String := "unset";
-- Canary for when a string switch is given without value

subtype GNAT_String is GNAT.Strings.String_Access;
-- Convenience for commands that use string arguments

function Is_Boolean is new AAA.Enum_Tools.Is_Valid (Boolean);

function To_Boolean (Image : GNAT_String;
Switch : String;
Default : Boolean)
return Boolean
with Post =>
(if Image in null or else Image.all = "" or else Image.all = Unset
then To_Boolean'Result = Default);
-- Convert a switch value to a boolean, if explicitly given, or use the
-- default otherwise. If not a valid boolean or empty, raise Checked_Error
-- with an appropriate error message.

end Alr.Commands;

0 comments on commit 0552e9d

Please sign in to comment.