@@ -19,30 +19,37 @@ pip install rocrate-action-recorder
1919
2020## Usage
2121
22+ Shown is an example of recording a CLI command (` example-cli input.txt output.txt ` ) implemented with ` argparse ` .
23+
2224``` python
2325import argparse
2426from datetime import datetime
2527from pathlib import Path
26- from rocrate_action_recorder import record_with_argparse, IOs
28+ from rocrate_action_recorder import record_with_argparse, IOArgumentNames
2729
30+ # Create an argparse parser
2831parser = argparse.ArgumentParser(prog = " example-cli" , description = " Example CLI" )
2932parser.add_argument(" --version" , action = " version" , version = " 1.2.3" )
3033parser.add_argument(" input" , type = Path, help = " Input file" )
3134parser.add_argument(" output" , type = Path, help = " Output file" )
3235
36+ # Prepare input
3337Path(" input.txt" ).write_text(" hello" )
38+
39+ # Parse arguments
3440args = [' input.txt' , ' output.txt' ]
3541ns = parser.parse_args(args)
36- # Tell recorder about input and output files
37- ios = IOs(input_files = [" input" ], output_files = [" output" ])
38- start_time = datetime.now()
3942
40- # Do handling of the CLI command here...
43+ # Do handling of the CLI command here
44+ start_time = datetime.now()
4145# For demonstration, just upper case input to output
4246Path(ns.output).write_text(ns.input.read_text().upper())
4347
4448record_with_argparse(
45- parser, ns, ios,
49+ parser,
50+ ns,
51+ # Tell recorder which arguments are for input and output files
52+ IOArgumentNames(input_files = [" input" ], output_files = [" output" ]),
4653 start_time,
4754 dataset_license = " CC-BY-4.0" ,
4855 # argv argument is optional, in real usage you can omit it
@@ -156,6 +163,64 @@ Will generate a `ro-crate-metadata.json` file in the current working directory d
156163
157164</details >
158165
166+
167+
168+ <details >
169+ <summary >
170+ You can also call the argument parser agnostic version of the recorder directly. (Click me to see code)
171+ </summary >
172+
173+ ``` python
174+ from datetime import datetime, UTC
175+ from pathlib import Path
176+
177+ from rocrate_action_recorder import (
178+ IOArgumentPath,
179+ IOArgumentPaths,
180+ Program,
181+ record,
182+ )
183+
184+ crate_dir = Path()
185+ input_path = crate_dir / " input.txt"
186+ output_path = crate_dir / " output.txt"
187+ input_path.write_text(" Hello World\n " )
188+ argv = [
189+ " myscript" ,
190+ " --input" ,
191+ str (input_path),
192+ " --output" ,
193+ str (output_path),
194+ ]
195+ start_time = datetime(2026 , 1 , 16 , 12 , 0 , 0 , tzinfo = UTC )
196+ # Simulate the script's main operation
197+ output_path.write_text(input_path.read_text().upper())
198+ end_time = datetime(2026 , 1 , 16 , 12 , 0 , 5 , tzinfo = UTC )
199+
200+ crate_meta = record(
201+ program = Program(
202+ name = " myscript" , description = " My test script" , version = " 1.2.3"
203+ ),
204+ ioargs = IOArgumentPaths(
205+ input_files = [
206+ IOArgumentPath(name = " input" , path = input_path, help = " Input file" )
207+ ],
208+ output_files = [
209+ IOArgumentPath(name = " output" , path = output_path, help = " Output file" )
210+ ],
211+ ),
212+ argv = argv,
213+ current_user = " tester" ,
214+ start_time = start_time,
215+ end_time = end_time,
216+ crate_dir = crate_dir,
217+ dataset_license = " CC-BY-4.0" ,
218+ )
219+ # crate_meta == Path("ro-crate-metadata.json")
220+ ```
221+
222+ </details >
223+
159224<!-- SPHINX-END -->
160225
161226## Example
0 commit comments