donnieparka

donnieparka

Can't run IO.inspect(args) in the first main(args) line

defmodule Createproj do
  @moduledoc """
  Documentation for `Createproj`.
  """
  def print_help do
    IO.puts(
      "Usage: newP" <>
        " -f filepath: creates a new project\n" <>
        "-b: adds boilerplate\n"
    )
  end

  def write_boilerplate(project_name) do
    file_name = project_name <> ".ex"
    file_path = Path.join([project_name, file_name])
    boilerplate = "defmodule #{project_name} do\nend"
    File.write(file_path, boilerplate, [:append])
  end

  def get_filename(args) do
    Enum.with_index(args)
    |> Enum.find(fn {arg, index} -> arg == "-f" and index < length(args) - 1 end)
    |> case do
      {_, index} ->
        Enum.at(args, index + 1)

      {nil} ->
        nil
    end
  end

  def create_folder(project_name, file_name \\ "") do
    folder_path = Path.join([project_name, file_name])

    File.mkdir(folder_path)
    |> case do
      :ok -> IO.puts("created#{folder_path}")
      {_, _} -> IO.puts("failed to make #{file_name}")
    end
  end

  def main(args) do
    IO.inspect(args)
    new_file = Enum.member?(args, "-f")
    boilerplate = Enum.member?(args, "-b")

    cond do
      new_file and not boilerplate ->
        project_name = get_filename(args)
        create_folder(project_name)
        create_folder(project_name, "/lib")
        create_folder(project_name, "/test")

      new_file and boilerplate ->
        project_name = get_filename(args)
        create_folder(project_name)
        create_folder(project_name, "/lib")
        create_folder(project_name, "/test")
        write_boilerplate(project_name)

      true ->
        print_help()
    end
  end
end

compilation works great but when I run:

~/projects ./createproj -f dio -b
** (MatchError) no match of right hand side value: "dio"
    (createproj 0.1.0) lib/createproj.ex:33: Createproj.main/1
    (elixir 1.18.1) lib/kernel/cli.ex:137: anonymous fn/3 in Kernel.CLI.exec_fun/2

If I run it from the iex like this:

Createproj.main(["-f", "cane", "-b"])
["-f", "cane", "-b"]
createdcane
createdcane/lib
createdcane/test
:ok

as you can see everything works… any idea what’s going on?

#io

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If this is an escript, you need to rebuild the escript after each change you make.

Also Liked

al2o3cr

al2o3cr

Line 33 of what you posted isn’t in main and couldn’t raise a MatchError anyways:

folder_path = Path.join([project_name, file_name])

My guess is that a compiled version of Createproj is running, confusing the situation.

Eiji

Eiji

I have tried this in newly generated project. I have only added escript: [main_module: Createproj] to Keyword returned in project function inside mix.exs and it works as expected. We would need something more to see where is the problem as for now I cannot reproduce it.

al2o3cr

al2o3cr

The BEAM caches compiled bytecode to disk when it reads .ex files, so rebooting will not make a difference. Usually the advice is to remove the _build folder, but I’m not sure where that lives for escripts.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement