Skip to content

Commit afe1270

Browse files
committed
Merge branch 'nightly'
2 parents 634cd9b + 1d0770c commit afe1270

File tree

8 files changed

+162
-66
lines changed

8 files changed

+162
-66
lines changed

.github/workflows/check_nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: checkout repo
13-
uses: actions/checkout@v2
13+
uses: actions/checkout@v4
1414
with:
1515
ref: nightly
1616
- name: install elan

Cli/Basic.lean

Lines changed: 82 additions & 46 deletions
Large diffs are not rendered by default.

Cli/Example.lean

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def runExampleCmd (p : Parsed) : IO UInt32 := do
1818
let priority : Nat := p.flag! "priority" |>.as! Nat
1919
IO.println <| "Flag `--priority` always has at least a default value: " ++ toString priority
2020

21+
if p.hasFlag "module" then
22+
let moduleName : ModuleName := p.flag! "module" |>.as! ModuleName
23+
IO.println <| s!"Flag `--module` was set to `{moduleName}`."
24+
2125
if let some setPathsFlag := p.flag? "set-paths" then
2226
IO.println <| toString <| setPathsFlag.as! (Array String)
2327
return 0
@@ -43,6 +47,9 @@ def exampleCmd : Cmd := `[Cli|
4347
o, optimize; "Declares a flag `--optimize` with an associated short alias `-o`."
4448
p, priority : Nat; "Declares a flag `--priority` with an associated short alias `-p` " ++
4549
"that takes an argument of type `Nat`."
50+
module : ModuleName; "Declares a flag `--module` that takes an argument of type `ModuleName` " ++
51+
"which can be used to reference Lean modules like `Init.Data.Array` " ++
52+
"or Lean files using a relative path like `Init/Data/Array.lean`."
4653
"set-paths" : Array String; "Declares a flag `--set-paths` " ++
4754
"that takes an argument of type `Array Nat`. " ++
4855
"Quotation marks allow the use of hyphens."
@@ -68,14 +75,15 @@ def exampleCmd : Cmd := `[Cli|
6875
def main (args : List String) : IO UInt32 :=
6976
exampleCmd.validate args
7077

71-
#eval main <| "-i -o -p 1 --set-paths=path1,path2,path3 input output1 output2".splitOn " "
78+
#eval main <| "-i -o -p 1 --module=Lean.Compiler --set-paths=path1,path2,path3 input output1 output2".splitOn " "
7279
/-
7380
Yields:
7481
Input: input
7582
Outputs: #[output1, output2]
7683
Flag `--invert` was set.
7784
Flag `--optimize` was set.
7885
Flag `--priority` always has at least a default value: 1
86+
Flag `--module` was set to `Lean.Compiler`.
7987
#[path1, path2, path3]
8088
-/
8189

@@ -121,6 +129,11 @@ Yields:
121129
-p, --priority : Nat Declares a flag `--priority` with an associated
122130
short alias `-p` that takes an argument of type
123131
`Nat`. [Default: `0`]
132+
--module : ModuleName Declares a flag `--module` that takes an
133+
argument of type `ModuleName` which can be used
134+
to reference Lean modules like `Init.Data.Array`
135+
or Lean files using a relative path like
136+
`Init/Data/Array.lean`.
124137
--set-paths : Array String Declares a flag `--set-paths` that takes an
125138
argument of type `Array Nat`. Quotation marks
126139
allow the use of hyphens.

Cli/Extensions.lean

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Cli.Basic
22

3+
namespace Cli
4+
35
section Utils
46
namespace Array
57
/--
@@ -8,7 +10,7 @@ section Utils
810
-/
911
def leftUnionBy [Ord α] (key : β → α) (left : Array β) (right : Array β)
1012
: Array β := Id.run do
11-
let leftMap := left.map (fun v => (key v, v)) |>.toList |> Std.RBMap.ofList (cmp := compare)
13+
let leftMap := left.map (fun v => (key v, v)) |>.toList |> Lean.RBMap.ofList (cmp := compare)
1214
let mut result := left
1315
for v in right do
1416
if ¬ leftMap.contains (key v) then
@@ -21,7 +23,7 @@ section Utils
2123
-/
2224
def rightUnionBy [Ord α] (key : β → α) (left : Array β) (right : Array β)
2325
: Array β := Id.run do
24-
let rightMap := right.map (fun v => (key v, v)) |>.toList |> Std.RBMap.ofList (cmp := compare)
26+
let rightMap := right.map (fun v => (key v, v)) |>.toList |> Lean.RBMap.ofList (cmp := compare)
2527
let mut result := right
2628
for v in left.reverse do
2729
if ¬ rightMap.contains (key v) then
@@ -31,13 +33,11 @@ section Utils
3133
/-- Deletes all elements from `left` whose `key` is in `right`. -/
3234
def diffBy [Ord α] (key : β → α) (left : Array β) (right : Array α)
3335
: Array β :=
34-
let rightMap := Std.RBTree.ofList (cmp := compare) right.toList
36+
let rightMap := Lean.RBTree.ofList (cmp := compare) right.toList
3537
left.filter fun v => ¬ (rightMap.contains <| key v)
3638
end Array
3739
end Utils
3840

39-
namespace Cli
40-
4141
section Extensions
4242
/-- Prepends an author name to the description of the command. -/
4343
def author (author : String) : Extension := {
@@ -47,8 +47,8 @@ section Extensions
4747
/-- Appends a longer description to the end of the help. -/
4848
def longDescription (description : String) : Extension := {
4949
extend := fun cmd => cmd.update (furtherInformation? :=
50-
some <| cmd.furtherInformation?.optStr ++ lines #[
51-
cmd.furtherInformation?.optStr,
50+
some <| Option.optStr cmd.furtherInformation? ++ lines #[
51+
Option.optStr cmd.furtherInformation?,
5252
(if cmd.hasFurtherInformation then "\n" else "") ++ renderSection "DESCRIPTION" description
5353
]
5454
)
@@ -58,7 +58,7 @@ section Extensions
5858
def helpSubCommand : Extension := {
5959
priority := 0
6060
extend := fun cmd =>
61-
let helpCmd := .mk
61+
let helpCmd := .mk
6262
(parent := cmd)
6363
(name := "help")
6464
(version? := none)
@@ -67,7 +67,7 @@ section Extensions
6767
-- adding it once without a command handler ensures that the help will include
6868
-- the help subcommand itself
6969
let cmd := cmd.update (subCmds := cmd.subCmds.push helpCmd)
70-
let helpCmd := helpCmd.update (run := fun _ => do
70+
let helpCmd := helpCmd.update (run := fun _ => do
7171
cmd.toFullCmdWithoutExtensions.printHelp
7272
return 0)
7373
let subCmds := cmd.subCmds.set! (cmd.subCmds.size - 1) helpCmd
@@ -80,12 +80,12 @@ section Extensions
8080
if cmd.version?.isNone then
8181
panic! "Cli.versionSubCommand!: Cannot add `version` subcommand to command without a version."
8282
else
83-
let helpCmd := .mk
83+
let helpCmd := .mk
8484
(parent := cmd)
8585
(name := "version")
8686
(version? := none)
8787
(description := "Prints the version.")
88-
(run := fun _ => do
88+
(run := fun _ => do
8989
cmd.toFullCmdWithoutExtensions.printVersion!
9090
return 0)
9191
cmd.update (subCmds := cmd.subCmds.push helpCmd)
@@ -111,7 +111,7 @@ section Extensions
111111
cmd.update (flags := newMetaFlags)
112112
postprocess := fun cmd parsed =>
113113
let defaultFlags := findDefaultFlags cmd
114-
return { parsed with flags := parsed.flags.leftUnionBy (·.flag.longName) defaultFlags }
114+
return { parsed with flags := Array.leftUnionBy (·.flag.longName) parsed.flags defaultFlags }
115115
}
116116

117117
/--
@@ -134,7 +134,7 @@ section Extensions
134134
if parsed.hasFlag "help" ∨ parsed.hasFlag "version" then
135135
return parsed
136136
let requiredFlags := findRequiredFlags cmd
137-
let missingFlags := requiredFlags.diffBy (·.longName) <| parsed.flags.map (·.flag.longName)
137+
let missingFlags := Array.diffBy (·.longName) requiredFlags <| parsed.flags.map (·.flag.longName)
138138
if let some missingFlag ← pure <| missingFlags.get? 0 then
139139
throw s!"Missing required flag `--{missingFlag.longName}`."
140140
return parsed

Cli/Tests.lean

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,38 @@ section Info
388388
== "testsubcommand1 [0.0.1]\na properly short description\n\nUSAGE:\n testsubcommand1 [SUBCOMMAND] [FLAGS] <city-location>\n\nFLAGS:\n -h, --help Prints this message.\n --version Prints the version.\n --launch-the-nukes please avoid passing this flag at all costs.\n if you like, you can have newlines in descriptions.\n\nARGS:\n city-location : String can also use hyphens\n\nSUBCOMMANDS:\n testsubsubcommand does this even do anything?\n version Prints the version.\n help Prints this message."
389389
end Info
390390

391+
section ModuleName
392+
def ModuleName.parse? : String → Option ModuleName := ParseableType.parse?
393+
394+
section ValidInputs
395+
#eval ModuleName.parse? "Lean.Mathlib.Data" == some `Lean.Mathlib.Data
396+
#eval ModuleName.parse? "F00Bar.BarF00" == some `F00Bar.BarF00
397+
#eval ModuleName.parse? "foo_bar" == some `foo_bar
398+
#eval ModuleName.parse? "asdf.«foo bar»" == some `asdf.«foo bar»
399+
#eval ModuleName.parse? "«1».«2»" == some `«1».«2»
400+
#eval ModuleName.parse? "« »" == some `« »
401+
#eval ModuleName.parse? "Lean/Mathlib/Data/Afile.lean" == some `Lean.Mathlib.Data.Afile
402+
#eval ModuleName.parse? "Foo.lean" == some `Foo
403+
#eval ModuleName.parse? "..lean" == some `«.»
404+
#eval ModuleName.parse? " .lean" == some `« »
405+
#eval ModuleName.parse? " /Foo.lean" == some `« ».Foo
406+
end ValidInputs
407+
408+
section InvalidInputs
409+
#eval ModuleName.parse? "" == none
410+
#eval ModuleName.parse? "." == none
411+
#eval ModuleName.parse? ".asdf" == none
412+
#eval ModuleName.parse? "asdf." == none
413+
#eval ModuleName.parse? "1.asdf" == none
414+
#eval ModuleName.parse? "asdf.1" == none
415+
#eval ModuleName.parse? "1asdf" == none
416+
#eval ModuleName.parse? "foo bar" == none
417+
#eval ModuleName.parse? "foo,bar" == none
418+
#eval ModuleName.parse? "«»" == none
419+
#eval ModuleName.parse? "x.«»" == none
420+
#eval ModuleName.parse? ".lean" == none
421+
#eval ModuleName.parse? "/foo.lean" == none
422+
end InvalidInputs
423+
end ModuleName
424+
391425
end Cli

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
See [the documentation of Lake](https://github.com/leanprover/lake).
44

55
Use one of the following for the `<tag>` in the dependency source `Source.git "https://github.com/mhuisi/lean4-cli.git" "<tag>"`:
6-
- `main` if you want to stay in sync with Lean 4 milestone releases. The `main` branch will contain a working version of lean4-cli for the most recent Lean 4 milestone.
6+
- `main` if you want to stay in sync with Lean 4 stable releases. The `main` branch will contain a working version of lean4-cli for the most recent Lean 4 stable release.
77
- `nightly` if you want to stay in sync with Lean 4 nightly releases. The `nightly` branch will contain a working version of lean4-cli for the most recent Lean 4 nightly build.
8-
- One of the specific release tags if you want to pin a specific version, e.g. `v1.0.0-lv4.0.0-m4` for v1.0.0 for the 4th Lean 4 milestone release or `v1.0.0-lnightly-2022-05-21` for v1.0.0 for the Lean 4 nightly version from 2022-05-21. Only nightlies where lean4-cli broke will receive a release tag.
8+
- One of the specific release tags if you want to pin a specific version, e.g. `v2.2.0-lv4.0.0` for v2.2.0 of lean4-cli and the Lean 4 stable release with version v4.0.0 or `v2.2.0-lnightly-2023-08-23` for v2.2.0 of lean4-cli and the Lean 4 nightly version from 2023-08-23.
99

1010
### Configuration
1111
Commands are configured with a lightweight DSL. The following declarations define a command `exampleCmd` with two subcommands `installCmd` and `testCmd`. `runExampleCmd` denotes a handler that is called when the command is run and is described further down below in the **Command Handlers** subsection.
@@ -34,6 +34,9 @@ def exampleCmd : Cmd := `[Cli|
3434
o, optimize; "Declares a flag `--optimize` with an associated short alias `-o`."
3535
p, priority : Nat; "Declares a flag `--priority` with an associated short alias `-p` " ++
3636
"that takes an argument of type `Nat`."
37+
module : ModuleName; "Declares a flag `--module` that takes an argument of type `ModuleName` " ++
38+
"which be can used to reference Lean modules like `Init.Data.Array` " ++
39+
"or Lean files using a relative path like `Init/Data/Array.lean`."
3740
"set-paths" : Array String; "Declares a flag `--set-paths` " ++
3841
"that takes an argument of type `Array Nat`. " ++
3942
"Quotation marks allow the use of hyphens."
@@ -77,6 +80,10 @@ def runExampleCmd (p : Parsed) : IO UInt32 := do
7780
let priority : Nat := p.flag! "priority" |>.as! Nat
7881
IO.println <| "Flag `--priority` always has at least a default value: " ++ toString priority
7982
83+
if p.hasFlag "module" then
84+
let moduleName : ModuleName := p.flag! "module" |>.as! ModuleName
85+
IO.println <| s!"Flag `--module` was set to `{moduleName}`."
86+
8087
if let some setPathsFlag := p.flag? "set-paths" then
8188
IO.println <| toString <| setPathsFlag.as! (Array String)
8289
return 0
@@ -89,14 +96,15 @@ Below you can find some simple examples of how to pass user input to the Cli lib
8996
def main (args : List String) : IO UInt32 :=
9097
exampleCmd.validate args
9198
92-
#eval main <| "-i -o -p 1 --set-paths=path1,path2,path3 input output1 output2".splitOn " "
99+
#eval main <| "-i -o -p 1 --module=Lean.Compiler --set-paths=path1,path2,path3 input output1 output2".splitOn " "
93100
/-
94101
Yields:
95102
Input: input
96103
Outputs: #[output1, output2]
97104
Flag `--invert` was set.
98105
Flag `--optimize` was set.
99106
Flag `--priority` always has at least a default value: 1
107+
Flag `--module` was set to `Lean.Compiler`.
100108
#[path1, path2, path3]
101109
-/
102110
@@ -143,6 +151,11 @@ FLAGS:
143151
-p, --priority : Nat Declares a flag `--priority` with an associated
144152
short alias `-p` that takes an argument of type
145153
`Nat`. [Default: `0`]
154+
--module : ModuleName Declares a flag `--module` that takes an
155+
argument of type `ModuleName` which can be used
156+
to reference Lean modules like `Init.Data.Array`
157+
or Lean files using a relative path like
158+
`Init/Data/Array.lean`.
146159
--set-paths : Array String Declares a flag `--set-paths` that takes an
147160
argument of type `Array Nat`. Quotation marks
148161
allow the use of hyphens.

lakefile.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ open Lake DSL
33

44
package Cli
55

6-
@[defaultTarget]
6+
@[default_target]
77
lean_lib Cli

lean-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
leanprover/lean4:v4.0.0-m5
1+
leanprover/lean4:nightly-2023-08-23

0 commit comments

Comments
 (0)