@@ -26,7 +26,9 @@ module fpm_command_line
2626use fpm_environment, only : get_os_type, get_env, &
2727 OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
2828 OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD
29+ use fpm_error, only : error_t
2930use M_CLI2, only : set_args, lget, sget, unnamed, remaining, specified
31+ use fpm_os, only : change_directory, get_current_directory
3032use fpm_strings, only : lower, split, fnv_1a
3133use fpm_filesystem, only : basename, canon_path, to_fortran_name
3234use fpm_compiler, only : get_default_compile_flags
@@ -119,6 +121,9 @@ subroutine get_command_line_settings(cmd_settings)
119121 integer :: i
120122 integer :: widest
121123 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
122127
123128 call set_help()
124129 ! text for --version switch,
@@ -148,12 +153,16 @@ subroutine get_command_line_settings(cmd_settings)
148153 if (adjustl (cmdarg(1 :1 )) .ne. ' -' )exit
149154 enddo
150155
156+ call get_current_directory(pwd_start)
157+
158+ common_args = ' --directory:C " " '
159+
151160 ! now set subcommand-specific help text and process commandline
152161 ! arguments. Then call subcommand routine
153162 select case (trim (cmdarg))
154163
155164 case (' run' )
156- call set_args(' &
165+ call set_args(common_args // ' &
157166 & --target " " &
158167 & --list F &
159168 & --all F &
@@ -206,7 +215,7 @@ subroutine get_command_line_settings(cmd_settings)
206215 & verbose= lget(' verbose' ) )
207216
208217 case (' build' )
209- call set_args( ' &
218+ call set_args(common_args // ' &
210219 & --profile " " &
211220 & --list F &
212221 & --show-model F &
@@ -228,7 +237,7 @@ subroutine get_command_line_settings(cmd_settings)
228237 & verbose= lget(' verbose' ) )
229238
230239 case (' new' )
231- call set_args(' &
240+ call set_args(common_args // ' &
232241 & --src F &
233242 & --lib F &
234243 & --app F &
@@ -298,7 +307,7 @@ subroutine get_command_line_settings(cmd_settings)
298307 endif
299308
300309 case (' help' ,' manual' )
301- call set_args(' &
310+ call set_args(common_args // ' &
302311 & --verbose F &
303312 & ' ,help_help,version_text)
304313 if (size (unnamed).lt. 2 )then
@@ -346,7 +355,8 @@ subroutine get_command_line_settings(cmd_settings)
346355 call printhelp(help_text)
347356
348357 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 " " &
350360 & --list F &
351361 & --compiler "' // get_env(' FPM_COMPILER' ,' gfortran' )// ' " &
352362 & --flag:: " "&
@@ -371,7 +381,7 @@ subroutine get_command_line_settings(cmd_settings)
371381 call move_alloc(install_settings, cmd_settings)
372382
373383 case (' list' )
374- call set_args(' &
384+ call set_args(common_args // ' &
375385 & --list F&
376386 & --verbose F&
377387 &' , help_list, version_text)
@@ -380,7 +390,7 @@ subroutine get_command_line_settings(cmd_settings)
380390 call printhelp(help_list_dash)
381391 endif
382392 case (' test' )
383- call set_args(' &
393+ call set_args(common_args // ' &
384394 & --target " " &
385395 & --list F&
386396 & --profile " "&
@@ -425,7 +435,7 @@ subroutine get_command_line_settings(cmd_settings)
425435 & verbose= lget(' verbose' ) )
426436
427437 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' , &
429439 help_update, version_text)
430440
431441 if ( size (unnamed) .gt. 1 )then
@@ -441,7 +451,7 @@ subroutine get_command_line_settings(cmd_settings)
441451
442452 case default
443453
444- call set_args(' &
454+ call set_args(common_args // ' &
445455 & --list F&
446456 & --verbose F&
447457 &' , help_fpm, version_text)
@@ -462,6 +472,18 @@ subroutine get_command_line_settings(cmd_settings)
462472 call printhelp(help_text)
463473
464474 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+
465487 contains
466488
467489 subroutine check_build_vals ()
@@ -674,6 +696,8 @@ subroutine set_help()
674696 ' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] [options]' , &
675697 ' ' , &
676698 ' SUBCOMMAND OPTIONS ' , &
699+ ' -C, --directory PATH' , &
700+ ' Change working directory to PATH before running any command' , &
677701 ' --profile PROF selects the compilation profile for the build.' ,&
678702 ' Currently available profiles are "release" for' ,&
679703 ' high optimization and "debug" for full debug options.' ,&
0 commit comments