How to get `build_flag` value in `pre:` build script?

I am trying to get value of build_flag in pre: build script.

I can get python string list of BUILD_FLAGS by env.get("BUILD_FLAGS), Is there any function available that can return the value of specified BUILD_FLAG is available?

Is there any function that can be used to remove/modify BUILD_FLAGS similar to ProcessUnFlags?

Do you mean raw build_flags value from platformio.ini? See example with ConfigParser Redirecting...

2 Likes

Yes, This works fine if all build_flags has a value assigned.
It fails when there is a define like -D ENABLE_DEBUG with error too many values to unpack:.

However, filterning list by the type list does the job.

def get_build_flag_value(flag_name):
    build_flags = env.ParseFlags(env['BUILD_FLAGS'])
    flags_with_value_list = [build_flag for build_flag in build_flags.get('CPPDEFINES') if type(build_flag) == list]
    defines = {k: v for (k, v) in flags_with_value_list}
    return defines.get(flag_name)
1 Like