@@ -26,7 +26,9 @@ module fpm_command_line
26
26
use fpm_environment, only : get_os_type, get_env, &
27
27
OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
28
28
OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD
29
+ use fpm_error, only : error_t
29
30
use M_CLI2, only : set_args, lget, sget, unnamed, remaining, specified
31
+ use fpm_os, only : change_directory, get_current_directory
30
32
use fpm_strings, only : lower, split, fnv_1a
31
33
use fpm_filesystem, only : basename, canon_path, to_fortran_name
32
34
use fpm_compiler, only : get_default_compile_flags
@@ -119,6 +121,9 @@ subroutine get_command_line_settings(cmd_settings)
119
121
integer :: i
120
122
integer :: widest
121
123
type (fpm_install_settings), allocatable :: install_settings
124
+ character (len= :), allocatable :: pwd_start, working_dir
125
+ character (len= :), allocatable :: common_args
126
+ type (error_t), allocatable :: error
122
127
123
128
call set_help()
124
129
! text for --version switch,
@@ -148,12 +153,16 @@ subroutine get_command_line_settings(cmd_settings)
148
153
if (adjustl (cmdarg(1 :1 )) .ne. ' -' )exit
149
154
enddo
150
155
156
+ call get_current_directory(pwd_start)
157
+
158
+ common_args = ' --directory:C " " '
159
+
151
160
! now set subcommand-specific help text and process commandline
152
161
! arguments. Then call subcommand routine
153
162
select case (trim (cmdarg))
154
163
155
164
case (' run' )
156
- call set_args(' &
165
+ call set_args(common_args // ' &
157
166
& --target " " &
158
167
& --list F &
159
168
& --all F &
@@ -206,7 +215,7 @@ subroutine get_command_line_settings(cmd_settings)
206
215
& verbose= lget(' verbose' ) )
207
216
208
217
case (' build' )
209
- call set_args( ' &
218
+ call set_args(common_args // ' &
210
219
& --profile " " &
211
220
& --list F &
212
221
& --show-model F &
@@ -228,7 +237,7 @@ subroutine get_command_line_settings(cmd_settings)
228
237
& verbose= lget(' verbose' ) )
229
238
230
239
case (' new' )
231
- call set_args(' &
240
+ call set_args(common_args // ' &
232
241
& --src F &
233
242
& --lib F &
234
243
& --app F &
@@ -298,7 +307,7 @@ subroutine get_command_line_settings(cmd_settings)
298
307
endif
299
308
300
309
case (' help' ,' manual' )
301
- call set_args(' &
310
+ call set_args(common_args // ' &
302
311
& --verbose F &
303
312
& ' ,help_help,version_text)
304
313
if (size (unnamed).lt. 2 )then
@@ -346,7 +355,8 @@ subroutine get_command_line_settings(cmd_settings)
346
355
call printhelp(help_text)
347
356
348
357
case (' install' )
349
- call set_args(' --profile " " --no-rebuild F --verbose F --prefix " " &
358
+ call set_args(common_args // ' &
359
+ & --profile " " --no-rebuild F --verbose F --prefix " " &
350
360
& --list F &
351
361
& --compiler "' // get_env(' FPM_COMPILER' ,' gfortran' )// ' " &
352
362
& --flag:: " "&
@@ -371,7 +381,7 @@ subroutine get_command_line_settings(cmd_settings)
371
381
call move_alloc(install_settings, cmd_settings)
372
382
373
383
case (' list' )
374
- call set_args(' &
384
+ call set_args(common_args // ' &
375
385
& --list F&
376
386
& --verbose F&
377
387
&' , help_list, version_text)
@@ -380,7 +390,7 @@ subroutine get_command_line_settings(cmd_settings)
380
390
call printhelp(help_list_dash)
381
391
endif
382
392
case (' test' )
383
- call set_args(' &
393
+ call set_args(common_args // ' &
384
394
& --target " " &
385
395
& --list F&
386
396
& --profile " "&
@@ -425,7 +435,7 @@ subroutine get_command_line_settings(cmd_settings)
425
435
& verbose= lget(' verbose' ) )
426
436
427
437
case (' update' )
428
- call set_args(' --fetch-only F --verbose F --clean F' , &
438
+ call set_args(common_args // ' --fetch-only F --verbose F --clean F' , &
429
439
help_update, version_text)
430
440
431
441
if ( size (unnamed) .gt. 1 )then
@@ -441,7 +451,7 @@ subroutine get_command_line_settings(cmd_settings)
441
451
442
452
case default
443
453
444
- call set_args(' &
454
+ call set_args(common_args // ' &
445
455
& --list F&
446
456
& --verbose F&
447
457
&' , help_fpm, version_text)
@@ -462,6 +472,18 @@ subroutine get_command_line_settings(cmd_settings)
462
472
call printhelp(help_text)
463
473
464
474
end select
475
+
476
+ ! Change working directory if requested
477
+ working_dir = sget(" directory" )
478
+ if (len_trim (working_dir) > 0 ) then
479
+ call change_directory(working_dir, error)
480
+ if (allocated (error)) then
481
+ write (stderr, ' (*(a, 1x))' ) " <ERROR>" , error% message
482
+ stop 1
483
+ end if
484
+ write (stdout, ' (*(a))' ) " fpm: Entering directory '" // working_dir// " '"
485
+ end if
486
+
465
487
contains
466
488
467
489
subroutine check_build_vals ()
@@ -674,6 +696,8 @@ subroutine set_help()
674
696
' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] [options]' , &
675
697
' ' , &
676
698
' SUBCOMMAND OPTIONS ' , &
699
+ ' -C, --directory PATH' , &
700
+ ' Change working directory to PATH before running any command' , &
677
701
' --profile PROF selects the compilation profile for the build.' ,&
678
702
' Currently available profiles are "release" for' ,&
679
703
' high optimization and "debug" for full debug options.' ,&
0 commit comments