diff --git a/redcap_bridge/cli.py b/redcap_bridge/cli.py index 65b375ad..3ec59295 100644 --- a/redcap_bridge/cli.py +++ b/redcap_bridge/cli.py @@ -1,8 +1,10 @@ """ Provide CLI for the main functionalities of the redcap bridge """ +import sys from redcap_bridge.server_interface import download_records +from redcap_bridge.server_elab_interface import download_experiment def main(command_line=None): @@ -19,6 +21,8 @@ def main(command_line=None): ) subparsers = parser.add_subparsers(dest='command') + + # Download command download = subparsers.add_parser('download', help='Downloads the data records') download.add_argument("destination", nargs=1, metavar='destination', type=str, help="The destination filename.") @@ -28,18 +32,29 @@ def main(command_line=None): help="Format to store the data (json/csv)") download.add_argument("-c", "--compressed", action='store_true', help="Compress the output file (use labels and merge checkbox columns)") + download.add_argument("-s", "--server", type=str, nargs=1, metavar='server', + choices=['redcap', 'elabftw'], help="The two server choices are redcap or elabftw", + default='redcap') + download.add_argument("experiment_id", nargs='?', metavar='experiment_id', type=str, + help="Experiment id.") # parse arguments args = parser.parse_args(command_line) if args.debug: print("debug: " + str(args)) + if args.command == 'download': if not args.format: args.format = ['csv'] - download_records(args.destination[0], args.config_json[0], format=args.format[0], - compressed=bool(args.compressed)) + if args.server == 'elabftw': + if not args.experiment_id: + parser.error("The experiment_id argument is required when --server elabftw is specified.") + download_experiment(args.config_json[0], args.experiment_id[0]) + else: + download_records(args.destination[0], args.config_json[0], format=args.format[0], + compressed=bool(args.compressed)) if __name__ == '__main__': diff --git a/redcap_bridge/test_redcap/test_cli.py b/redcap_bridge/test_redcap/test_cli.py index 6fc60774..33e25e17 100644 --- a/redcap_bridge/test_redcap/test_cli.py +++ b/redcap_bridge/test_redcap/test_cli.py @@ -50,3 +50,10 @@ def test_download(initialize_test_dir): stdout=subprocess.PIPE) assert 'error' not in str(result.stdout) assert pathlib.Path(output_file).exists() + + # download with Elabftw server + result = subprocess.run(['RedCapBridge', 'download', SERVER_CONFIG_YAML, '--server', 'elabftw', '232'], + stdout=subprocess.PIPE) + assert 'error' not in str(result.stdout) + assert pathlib.Path(output_file).exists() +